MVC 5 AttributeRouting捕获全部 [英] MVC 5 AttributeRouting Catch All

查看:84
本文介绍了MVC 5 AttributeRouting捕获全部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在MVC中使用新的属性路由"创建全部捕获"路由

How do I create a catch all route with the new Attribute routing in MVC

我尝试了这个: [Route("{pagenode}", Order = 999)]

但是当我有一条命名的路线时 [Route("contact"]

But when I have a named route like [Route("contact"]

我收到"Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL."错误.

推荐答案

您不能通过属性路由来做到这一点,请通过MVC4方式做到这一点:

You can't do this with Attribute routing, do this the MVC4 way:

像这样在您的routemapper中映射一条路线:

Map a route in your routemapper like this:

routes.MapRoute("RouteName","{*url}",new { controller = "YourFancyController", action = "YourAction" });

这将是您的全部路线.

如果您想将所有路由映射到其控制器,则可以执行以下操作:

If you would like to map all the routes to their controller you can do this:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapMvcAttributeRoutes();

    AreaRegistration.RegisterAllAreas();

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}

这篇关于MVC 5 AttributeRouting捕获全部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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