asp.net-mvc 属性路由在区域中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20466960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Attribute Routing not working in areas
提问by Thomas Stock
Scenario: I have a Forms area in my ASP.NET MVC 5 site.
场景:我的 ASP.NET MVC 5 站点中有一个表单区域。
I'm trying to redirect to the Details Action which uses a custom route defined using the new Attribute Routing feature.
我正在尝试重定向到使用使用新属性路由功能定义的自定义路由的详细信息操作。
The RedirectToAction:
RedirectToAction:
return RedirectToAction("Details", new { slug });
The action I'm redirecting to:
我要重定向到的操作:
[HttpGet]
[Route("forms/{slug}")]
public ActionResult Details(string slug)
{
var form = FormRepository.Get(slug);
...
return View(model);
}
I would expect a redirect to http://localhost/forms/my-slugbut instead the app is redirecting me to http://localhost/Forms/Details?slug=my-slug.
我希望重定向到,http://localhost/forms/my-slug但应用程序将我重定向到http://localhost/Forms/Details?slug=my-slug.
This means that the attribute routing is not working.
这意味着属性路由不起作用。
How can this be solved?
如何解决这个问题?
I have added the routes.MapMvcAttributeRoutes(); line to my RouteConfig:
我添加了 routes.MapMvcAttributeRoutes(); 线到我的 RouteConfig:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
And here's my Application_Start():
这是我的 Application_Start():
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
回答by Thomas Stock
You are probably combining convention based routing with attribute routing, and you should register your areas after you map the attribute routes.
您可能正在将基于约定的路由与属性路由相结合,并且您应该在映射属性路由后注册您的区域。
The line
线
AreaRegistration.RegisterAllAreas();
should be called AFTER this line:
应在此行之后调用:
routes.MapMvcAttributeRoutes();
The explanation (from http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx#route-areas):
If you are using both Areas with route attributes, and areas with convention based routes (set by an AreaRegistration class), then you need to make sure that area registration happen after MVC attribute routes are configured, however before the default convention-based route is set. The reason is that route registration should be ordered from the most specific (attributes) through more general (area registration) to the mist generic (the default route) to avoid generic routes from “hiding” more specific routes by matching incoming requests too early in the pipeline.
如果您同时使用具有路由属性的区域和具有基于约定的路由的区域(由 AreaRegistration 类设置),那么您需要确保在配置 MVC 属性路由之后发生区域注册,但是在默认基于约定的路由之前放。原因是路由注册应该从最具体的(属性)到更一般的(区域注册)到雾通用(默认路由),以避免通用路由通过过早匹配传入请求来“隐藏”更具体的路由管道。
When you create a blank asp.net mvc website, add an area and start using attribute routing, you will encounter this problem because the "Add Area" action in visual studio adds the RegisterAllAreas call in your Application_Start, before the route configuration..
当您创建一个空白的asp.net mvc 网站,添加一个区域并开始使用属性路由时,您会遇到此问题,因为visual studio 中的“添加区域”操作在路由配置之前在您的Application_Start 中添加了RegisterAllAreas 调用。
Alternative solution
替代方案
Perhaps you do not intend to keep using convention based routing and prefer to only use attribute routing. In this case you can just delete the FormsAreaRegistration.cs file.
也许您不打算继续使用基于约定的路由,而更喜欢仅使用属性路由。在这种情况下,您可以删除 FormsAreaRegistration.cs 文件。
回答by Michael Tranchida
Moving the AreaRegistration.RegisterAllAreas() to RouteConfig.cs wasn't enough for me. I also needed to use the AreaPrefix parameter for the RouteArea attibute:
将 AreaRegistration.RegisterAllAreas() 移动到 RouteConfig.cs 对我来说还不够。我还需要为 RouteArea 属性使用 AreaPrefix 参数:
//Use the named parameter "AreaPrefix"
[RouteArea("AreaName", AreaPrefix = "area-name-in-url")]
[RoutePrefix("controller-name-in-url")]
public class SampleController : Controller
{
[Route("{actionParameter}")]
public ActionResult Index(string actionParameter)
{
return View();
}
}
Edit:At some point, I came across a sample solution from Microsoft that nicely showed how to handle attribute routing. It also showed some nice examples of how to translate a SelectListinto an array of input[type="radio"]items as well as doing the same with an array of input[type="checkbox"]items (if I recall). This sample solution is probably a better answer to this question--as well as giving some good examples on displaying radio buttons and checkbox items. If anyone knows of this sample solution, please add a comment with a link to it.
编辑:在某些时候,我遇到了一个来自 Microsoft 的示例解决方案,它很好地展示了如何处理属性路由。它还展示了一些很好的示例,说明如何将 aSelectList转换为一组input[type="radio"]项目以及对一组input[type="checkbox"]项目执行相同操作(如果我记得的话)。这个示例解决方案可能是对这个问题的更好的回答——并且给出了一些关于显示单选按钮和复选框项目的很好的例子。如果有人知道此示例解决方案,请添加带有链接的评论。

