使用ASP.Net 5的MVC 6属性路由注册路由 [英] Registering routes with ASP.Net 5's MVC 6 Attribute Routing
问题描述
在ASP.Net MVC 5中使用属性路由时,您将调用命令routes.MapMvcAttributeRoutes();
,然后只需将Route()
标记添加到要为其构建路由的Controller/Actions中即可.我现在正尝试在ASP.Net MVC 6中执行此操作,并且发现了许多页面向您展示如何执行此操作,这与MVC 5中的操作确实没有什么不同,但是它们并未向您显示在何处或如何注册这些路由
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.
ASP.Net MVC 6是否只是为您自动提供它?还是有一个routes.MapMvcAttributeRoutes();
等效项,我必须在哪里调用?
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?
推荐答案
如果使用app.UseMvc()
在Startup类中启用了MVC,则您已经支持通过RouteAttribute
进行路由.
If you have enabled MVC in your Startup class using app.UseMvc()
, then you already have support for routing via the RouteAttribute
.
您只需将RouteAttribute
添加到其中一个控制器的方法中,例如:
You can just add the RouteAttribute
to a method of one of your controllers, e.g.:
[Route("/example")]
public IActionResult Example()
{
// …
}
这将使路由在/example
下可用,而不是默认的/ControllerName/Example
.
That makes the route available under /example
instead of the default /ControllerName/Example
.
您还可以在控制器类上使用RouteAttribute
,例如
You can also use the RouteAttribute
on the controller class, e.g.
[Route("test/[action]")]
public class ExampleController : Controller
{
// …
}
因此,操作将在/test/MethodName
下而不是Example/MethodName
下提供.
So actions would be available under /test/MethodName
instead of Example/MethodName
.
这篇关于使用ASP.Net 5的MVC 6属性路由注册路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!