Dotnet logo

The .NET Tools Blog

Essential productivity kit for .NET developers

.NET Tools How-To's

Work with ASP.NET Core route templates in ReSharper and Rider

Routing is at the core of ASP.NET development. In the latest 2021.1 EAP builds, ReSharper and Rider now offer some excellent new functionality to work with routes and route templates in ASP.NET. Let’s take a look what the .NET tools can do for you.

Structural navigation in route templates

Structural navigation allows you to navigate through code constructs without using the mouse or a complex key combination involving the arrow keys.

You can use Tab/Shift+Tab to move to the right and left. Place the cursor just to the left of the route attribute but before the opening square bracket, and structural navigation is activated. For example, to enable structural navigation in a Route attribute, place the cursor in between the [ and R. Then use Tab/Shift+Tab to navigate.

Indenting and outdenting are still available for those who want to use Tab/Shift+Tab for that purpose. Place the cursor at the beginning of the line or before the start of the route definition and use Tab/Shift+Tab.

Structural navigation for route templates

Route completion

Sometimes, it’s the little things that are the most helpful. Once you’ve declared a parameter in an action method, ReSharper or Rider inserts the route template of your choice.

Type the opening bracket {, then select the parameter to fill in. This is a great way to define routes quickly and without typos.

Autocomplete route templates

Add/remove route parameters

If a parameter is defined in an action method but not in the route template, you may want to add it to the template as well. You can quickly add and remove routing parameters by using Alt+Enter.

This works in reverse too. If a parameter is defined in a route template but not the action method, Alt+Enter adds it in as a parameter of the method.

Add route parameter to template

Rename refactoring parameters in route templates

As you might have expected, ReSharper and Rider offer rename refactoring for route parameters too. ReSharper and Rider users will love that standard rename refactoring is available for even the most complex route patterns and placeholder definitions. Notice that the parameter is renamed everywhere, not just the action method and parameter template (Alt+Enter).

Rename refactor works for route templates

Parameter type checks

ReSharper and Rider indicate the availability of type constraints that match an action method’s parameter type when a constraint has not yet been applied to a path template. Add the type constraint by using Alt+Enter.

In addition to the type, route templates can provide validation such as length, required, range, a regular expression, or a minimum or maximum value. Type a colon : after the type, and select a validation and select a validation option from the list that ReSharper or Rider provides.

Rider does route parameter type checks and validation

Extract to controller’s route template

Sometimes, routes defined on action methods work better at the controller level, as routes can be defined for all actions in the current controller. When this happens, Rider notifies you by displaying an intention indicator. Press Alt+Enter and extract the route template to the controller. You can then add route information to individual action methods.

Extract route templates to controller

Summary

Routing is an important aspect of ASP.NET web development. ReSharper and Rider have many great new features for building and managing routes in all ASP.NET project types.

Additionally, Rider has several quick fixes and helpers to keep your routes in order. Download Rider 2021.1 EAP and let us know if route building in ASP.NET is easier than ever before.

Discover more