使用ASP.Net 5的MVC 6属性路由注册路由 [英] Registering routes with ASP.Net 5's MVC 6 Attribute Routing

查看:121
本文介绍了使用ASP.Net 5的MVC 6属性路由注册路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆