I'm currently working on a console application that essentially mimics an ASP.NET Web API.
Everything was going smoothly until I ran into an issue regarding route attributes.
What I'm trying to achieve is something similar to the ASP.NET [Route] attribute, where you can define a route like this:
[Route("testFunc/{param}")]
In ASP.NET, this route pattern supports parameter placeholders (e.g., {param}), and Visual Studio even shows suggestions or highlights for parameters that match the method's signature.
However, when I try to implement my own custom Route attribute in a console application, I encounter limitations. For example, using string interpolation like this:
[MyRoute($"myFunc/{param}")
doesn’t work because attribute arguments in C# must be constant expressions, and interpolated strings are evaluated at runtime. This is a language constraint, not specific to ASP.NET.
My question is: is it even possible to replicate this kind of routing param highlighting behavior in a standard console app?
I’ve looked at the definition of the RouteAttribute in ASP.NET, but it doesn’t seem to provide much insight into how the route pattern parsing and parameter binding actually work under the hood.
Any suggestions or resources would be appreciated
Below, I’ve included the ASP.NET definition of the Route attribute for reference.
public class RouteAttribute : Attribute, IRouteTemplateProvider
{
private int? _order;
public RouteAttribute([StringSyntax("Route")] string template)
{
Template = template ?? throw new ArgumentNullException(nameof(template));
}
[StringSyntax("Route")]
public string Template { get; }
public int Order
{
get { return _order ?? 0; }
set { _order = value; }
}
int? IRouteTemplateProvider.Order => _order;
public string? Name { get; set; }
}
[Route]work the way it does for ASP.NET Core and how do I replicate it? Cause I think that's a bit too broadgreat project to learn about attributes, reflection, and HTTP request handling.not really. It takes a lot to make this "magic" work. Routing is handled by an entire middleware, not the attributes. Those are just markers. HTTP request handling has a lot of steps beyond routing. If you want to learn about it, check the ASP.NET Core Middleware docs. Routing is #5 in 8 different "filters" that process a request before it reaches your code