{"id":126567,"date":"2021-03-24T16:06:22","date_gmt":"2021-03-24T15:06:22","guid":{"rendered":"https:\/\/blog.jetbrains.com\/?post_type=dotnet&#038;p=126567"},"modified":"2021-04-13T15:30:08","modified_gmt":"2021-04-13T14:30:08","slug":"work-with-asp-net-core-route-templates-in-resharper-and-rider","status":"publish","type":"dotnet","link":"https:\/\/blog.jetbrains.com\/zh-hans\/dotnet\/2021\/03\/24\/work-with-asp-net-core-route-templates-in-resharper-and-rider","title":{"rendered":"Work with ASP.NET Core route templates in ReSharper and Rider"},"content":{"rendered":"<p>Routing is at the core of ASP.NET development. In the latest 2021.1 EAP builds, <strong>ReSharper and Rider now offer some excellent new functionality to work with routes and route templates in ASP.NET.<\/strong> Let\u2019s take a look what the .NET tools can do for you.<\/p>\n<h2>Route completion<\/h2>\n<p>Sometimes, it\u2019s the little things that are the most helpful. <strong>Once you\u2019ve declared a parameter in an action method, ReSharper or Rider inserts the route template of your choice.<\/strong> <\/p>\n<p>Type the opening bracket <code>{<\/code>, then select the parameter to fill in. This is a great way to define routes quickly and without typos.<\/p>\n<p><img decoding=\"async\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/03\/route-autocomplete.gif\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/03\/route-autocomplete.png\" alt=\"Autocomplete route templates\" width=\"554\" class=\"alignnone size-full wp-image-126916\" \/><\/p>\n<h2>Add\/remove route parameters<\/h2>\n<p>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. <strong>You can quickly add and remove routing parameters by using <kbd>Alt+Enter<\/kbd>.<\/strong><\/p>\n<p>This works in reverse too. If a parameter is defined in a route template but not the action method, <kbd>Alt+Enter<\/kbd> adds it in as a parameter of the method.<\/p>\n<p><img decoding=\"async\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/03\/route-add-param-to-template.gif\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/03\/route-add-param-to-template.png\" alt=\"Add route parameter to template\" width=\"500\" class=\"alignnone size-full wp-image-126621\" \/><\/p>\n<h2>Rename refactoring parameters in route templates<\/h2>\n<p>As you might have expected, <strong>ReSharper and Rider offer rename refactoring for route parameters too.<\/strong> 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 (<kbd>Alt+Enter<\/kbd>).<\/p>\n<p><img decoding=\"async\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/03\/route-rename-refactor.gif\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/03\/route-rename-refactor.png\" alt=\"Rename refactor works for route templates\" width=\"650\" class=\"alignnone size-full wp-image-126644\" \/><\/p>\n<h2>Parameter type checks<\/h2>\n<p>ReSharper and Rider indicate the availability of type constraints that match an action method&#8217;s parameter type when a constraint has not yet been applied to a path template. Add the type constraint by using <kbd>Alt+Enter<\/kbd>.<\/p>\n<p>In addition to the type, route templates can provide validation such as length, required, range, a regular expression, or a minimum or maximum value. <strong>Type a colon <code>:<\/code> after the type, and select a validation and select a validation option from the list that ReSharper or Rider provides.<\/strong><\/p>\n<p><img decoding=\"async\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/03\/route-param-type-check-plus-validation.gif\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/03\/route-param-type-check-plus-validation.png\" alt=\"Rider does route parameter type checks and validation\" width=\"614\" class=\"alignnone size-full wp-image-126666\" \/><\/p>\n<h2>Extract to controller\u2019s route template<\/h2>\n<p>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. <strong>Press <kbd>Alt+Enter<\/kbd> and extract the route template to the controller.<\/strong> You can then add route information to individual action methods.<\/p>\n<p><img decoding=\"async\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/03\/route-extract-to-controller.gif\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/03\/route-extract-to-controller.png\" alt=\"Extract route templates to controller\" width=\"614\" class=\"alignnone size-full wp-image-126688\" \/><\/p>\n<h2>Summary<\/h2>\n<p>Routing is an important aspect of ASP.NET web development. <strong>ReSharper and Rider have many great new features for building and managing routes in all ASP.NET project types.<\/strong><\/p>\n<p>Additionally, Rider has several quick fixes and helpers to keep your routes in order. <a href=\"https:\/\/www.jetbrains.com\/rider\/eap\/\" target=\"_blank\" rel=\"noopener\">Download Rider 2021.1 EAP<\/a> and let us know if route building in ASP.NET is easier than ever before.<\/p>\n","protected":false},"author":901,"featured_media":127552,"comment_status":"closed","ping_status":"closed","template":"","categories":[4992,1401],"tags":[2063,6286,756,46,1978,6524],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/dotnet\/126567"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/dotnet"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/types\/dotnet"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/users\/901"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/comments?post=126567"}],"version-history":[{"count":10,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/dotnet\/126567\/revisions"}],"predecessor-version":[{"id":136177,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/dotnet\/126567\/revisions\/136177"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media\/127552"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media?parent=126567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/categories?post=126567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/tags?post=126567"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/cross-post-tag?post=126567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}