Registering routes with ASP.Net 5's MVC 6 Attribute Routing

╄→尐↘猪︶ㄣ 提交于 2020-01-24 10:28:05

问题


When using Attribute Routing in ASP.Net MVC 5 you would call the command routes.MapMvcAttributeRoutes(); and then just add the Route() tag to the Controller/Actions you wanted to build routes to. I am now trying to do this in ASP.Net MVC 6 and have found many pages that show you how to do it, which is really no different than in MVC 5, but they don't show you where or how to register those routes.

Does ASP.Net MVC 6 just due it for you automatically or is there an equivalent to routes.MapMvcAttributeRoutes(); that I have to call some where?


回答1:


If you have enabled MVC in your Startup class using app.UseMvc(), then you already have support for routing via the RouteAttribute.

You can just add the RouteAttribute to a method of one of your controllers, e.g.:

[Route("/example")]
public IActionResult Example()
{
    // …
}

That makes the route available under /example instead of the default /ControllerName/Example.

You can also use the RouteAttribute on the controller class, e.g.

[Route("test/[action]")]
public class ExampleController : Controller
{
    // …
}

So actions would be available under /test/MethodName instead of Example/MethodName.



来源:https://stackoverflow.com/questions/34619316/registering-routes-with-asp-net-5s-mvc-6-attribute-routing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!