属性路由不工作领域 [英] Attribute Routing not working in areas

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

问题描述

场景:我在我的ASP.NET MVC 5个网站,一个窗体区域。

我想重定向到其采用采用全新的属性路由功能定义的自定义路线详细操作。

该RedirectToAction:

 返回RedirectToAction(详细信息,新的{蛞蝓});

动作我重定向到:

  [HTTPGET]
[路线(表格/ {}塞)]
公众的ActionResult详细信息(字符串蛞蝓)
{
    VAR形式= FormRepository.Get(塞);    ...    返回查看(模型);
}

我希望重定向到的http://本地主机/表格/我的段塞而是应用程序被重定向我 HTTP:/ /本地主机/表格/细节?蛞蝓= MY-塞

这意味着属性的路由不工作。

这又如何解决呢?

我已经加入了routes.MapMvcAttributeRoutes();行我RouteConfig:

 公共类RouteConfig
{
    公共静态无效的RegisterRoutes(RouteCollection路线)
    {
        routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);        routes.MapMvcAttributeRoutes();        routes.MapRoute(
            名称:默认,
            网址:{控制器} / {行动} / {ID}
            默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
        );
    }
}

和这里是我的Application_Start():

 保护无效的Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}


解决方案

您可能正在结合约定基于带属性的路由的路由,你应该的注册您的地区后,您映射属性的路由

  AreaRegistration.RegisterAllAreas();

应该被称为该行之后:

  routes.MapMvcAttributeRoutes();

从<一的解释( href=\"http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx#route-areas\">http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx#route-areas):


  

如果您使用的是带有路由属性这两个领域,并与基于约定路径(由AreaRegistration类设置)的区域,那么你需要确保区登记后发生的MVC属性的路由配置,但是默认convention-前基于路由设置。原因是这条路线的注册应该从最具体的(属性)通过更多的普通(区登记)的雾气一般(默认路由)通过匹配传入请求太早命令,以避免隐藏具体路由通用的路由管道。


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

替代解决方案

也许你不打算使用基于约定的路由和preFER只使用属性的路由保持。
在这种情况下,你可以删除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 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.

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天全站免登陆