How can I have lowercase routes in ASP.NET MVC?

2020-01-24 18:22发布

How can I have lowercase, plus underscore if possible, routes in ASP.NET MVC? So that I would have /dinners/details/2 call DinnersController.Details(2) and, if possible, /dinners/more_details/2 call DinnersController.MoreDetails(2)?

All this while still using patterns like {controller}/{action}/{id}.

8条回答
【Aperson】
2楼-- · 2020-01-24 19:18

This actually has two answers:

  1. You can already do this: the route engine does case-insensitive comparison. If you type a lower-case route, it will be routed to the appropriate controller and action.
  2. If you are using controls that generate route links (ActionLink, RouteLink, etc.) they will produce mixed-case links unless you override this default behavior.

You're on your own for the underscores, though...

查看更多
够拽才男人
3楼-- · 2020-01-24 19:26

If you happened to use ASP.NET Core, you probably should have a look at this:

Add the following line to the ConfigureServices method of the Startup class.

services.AddRouting(options => options.LowercaseUrls = true);
查看更多
登录 后发表回答