2

I am currently creating a custom .NET router. It is working as it should, but one thing that bothers me is that I don't get the same level of syntax highlight in rider as I get with other more standard methods.

for example if I use routes.MapRoute with a template string the variables gets highlighted in blue (as shown in the picture below)

missing syntax highlight

But I would like rider to understand that it should treat the string to ShopRouter the same.

Looking into the IRouteBuilder.MapRoute function I can see this

[StringSyntax("Route")] string? template

so I did the same to my constructor

public ShopRouter([StringSyntax("Route")] string template, object defaults, IRouter handler)

but it does nothing at all.

is it at all possible to get rider to understand this, or is it just a built in thing that I have no control over?

asked Oct 24, 2025 at 8:51

2 Answers 2

4

ReSharper does a lot of heavy lifting in terms of artificially adding attributes to existing Microsoft and .NET package methods which aids its own IDE hints, including string syntax highlighting: https://www.jetbrains.com/help/resharper/Code_Analysis__External_Annotations.html#jb

These are all just bundled directly with ReSharper, but you're also able to add them to your own methods with the JetBrains.Annotations nuget package. I think you'll find on the MapRoute method, there also appears to be a RouteTemplate attribute declared on that parameter which is probably one of the many that JetBrains has defined themselves. I suspect you'll also want to define that against your parameter to get highlighting in Rider.

In my test example, I've just got a method with the following signature

public static void TestMethodWithRoute([StringSyntax("Route"), RouteTemplate] string param)

Which produces the following syntax highlighting for me

Syntax-highlighted code sample

I'd probably suggest reflecting both attributes by including the StringSyntax("Route") one as well as I presume this is for Visual Studio, but that's entirely speculation and someone more educated than me in that area is welcome to correct me. I'm not sure how Visual Studio handles this kind of highlighting.

Also just a note, I did notice when I was experimenting with this that sometimes the UI needed a restart after adding the attribute to actually get the highlighting, but your experience may vary :)

answered Oct 24, 2025 at 10:36
Sign up to request clarification or add additional context in comments.

Comments

3

It seems that the highlighter relies on some rules being followed, for example it prefers instance methods, and sometimes specifically named methods (for local methods):

app.MapGet("/error", _ => throw new NotImplementedException("Hey"));
TestLocal("/error"); // no highlight
MapGet("/error"); // highlights
ShopRouter.MapGet("/error",_ => throw new NotImplementedException("Hey")); // highlights
ShopRouter.MapGet("/error"); // highlights
ShopRouter.MyName("/error",_ => throw new NotImplementedException("Hey")); // highlights
new ShopRouter("/error/{id}") // no highlight
 .Do("/api/{controller}/{action}"); // highlights
ShopRouter.Create("/api/{controller}/{action}"); // highlights
void TestLocal([StringSyntax("Route")] string pattern){}
void MapGet([StringSyntax("Route")] string pattern){}
class ShopRouter
{
 public ShopRouter([StringSyntax("Route")] string pattern) { }
 public void Do([StringSyntax("Route")] string pattern) => new ShopRouter(pattern);
 public static ShopRouter Create([StringSyntax("Route")] string pattern) => new ShopRouter(pattern);
 public static void MyName([StringSyntax("Route")] string pattern, RequestDelegate requestDelegate){}
 public static void MapGet([StringSyntax("Route")] string pattern, RequestDelegate requestDelegate){}
 public static void MapGet([StringSyntax("Route")] string pattern){}
}

Notes:

  1. For my setup it seems moving to a static method in class has helped
  2. This can be version-depended (I run Rider EAP on Linux + .NET 9) and can be quite brittle
  3. There is a similar feature in VS and it can behave in totally different way.
  4. You might want to look at plethora of annotations from the JetBrains.Annotations package, one of which could help you in this case

enter image description here

answered Oct 24, 2025 at 10:55

1 Comment

it is pretty interesting how it highlights some of that code. But feels like an antipattern to change the code structure that much just for an inconsistent highlight :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.