属性路由在区域中不起作用 [英] Attribute Routing not working in areas

查看:33
本文介绍了属性路由在区域中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:我的 ASP.NET MVC 5 站点中有一个表单区域.

我正在尝试重定向到详细信息操作,该操作使用使用新属性路由功能定义的自定义路由.

RedirectToAction:

return RedirectToAction("Details", new { slug });

我要重定向到的操作:

[HttpGet][路线(表格/{slug}")]公共 ActionResult 详细信息(字符串 slug){var form = FormRepository.Get(slug);...返回视图(模型);}

我希望重定向到 http://localhost/forms/my-slug 但应用程序将我重定向到 http://localhost/Forms/Details?slug=我的鼻涕虫.

这意味着属性路由不起作用.

如何解决?

我添加了 routes.MapMvcAttributeRoutes();线到我的 RouteConfig:

公共类RouteConfig{public static void RegisterRoutes(RouteCollection 路由){route.IgnoreRoute("{resource}.axd/{*pathInfo}");路线.MapMvcAttributeRoutes();路线.MapRoute(名称:默认",url: "{controller}/{action}/{id}",默认值:新 { 控制器 =主页",动作 =索引",id = UrlParameter.Optional });}}

这是我的 Application_Start():

protected void Application_Start(){AreaRegistration.RegisterAllAreas();FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);RouteConfig.RegisterRoutes(RouteTable.Routes);BundleConfig.RegisterBundles(BundleTable.Bundles);}

解决方案

您可能将基于约定的路由与属性路由相结合,并且您应该在映射属性路由后注册您的区域..>

线

AreaRegistration.RegisterAllAreas();

应该在这一行之后调用:

routes.MapMvcAttributeRoutes();

解释(来自https://devblogs.microsoft.com/aspnet/attribute-routing-in-asp-net-mvc-5/):

<块引用>

如果您同时使用具有路由属性的区域和具有基于约定的路由的区域(由 AreaRegistration 类设置),那么您需要确保在配置 MVC 属性路由之后发生区域注册,但在默认约定之前 -设置了基于路由.原因是路由注册应该从最具体的(属性)到更一般的(区域注册)到雾通用(默认路由),以避免通用路由通过过早匹配传入请求来隐藏"更具体的路由管道.

当你创建一个空白的asp.net mvc网站,添加一个区域并开始使用属性路由时,你会遇到这个问题,因为添加区域"Visual Studio 中的操作在路由配置之前在 Application_Start 中添加 RegisterAllAreas 调用..

替代解决方案

也许您不打算继续使用基于约定的路由,而更喜欢仅使用属性路由.在这种情况下,您可以删除 FormsAreaRegistration.cs 文件.

Scenario: I have a Forms area in my ASP.NET MVC 5 site.

I'm trying to redirect to the Details Action which uses a custom route defined using the new Attribute Routing feature.

The 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-slug but instead the app is redirecting me to 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:

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():

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

解决方案

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 https://devblogs.microsoft.com/aspnet/attribute-routing-in-asp-net-mvc-5/):

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.

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..

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.

这篇关于属性路由在区域中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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