【问题标题】:Regarding Route.config file and attribute routing ASP.Net MVC关于 Route.config 文件和属性路由 ASP.Net MVC
【发布时间】:2015-08-22 10:43:38
【问题描述】:

我们知道路由是在 route.config 文件中注册的,如下所示

routes.MapRoute(
    name: "ProductPage",
    url: "{productId}/{productTitle}",
    defaults: new { controller = "Products", action = "Show" },
    constraints: new { productId = "\\d+" }
);

我们可以从路由配置文件中删除路由相关代码并实现属性路由吗?

看到这个

[Route("{productId:int}/{productTitle}")]
public ActionResult Show(int productId) { ... }

我们可以使用上面的属性路由来代替吗? 我们可以从路由配置文件中删除所有与路由相关的代码,所以我的路由配置看起来像吗?

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

        routes.MapMvcAttributeRoutes();
    }
}

请帮助我控制此路由问题。谢谢

【问题讨论】:

  • Mvc5 升级就可以了。

标签: asp.net-mvc asp.net-mvc-4 routing


【解决方案1】:

当然可以,但最好在RouteConfig 中保留一个默认路由,并使用路由属性添加装饰其他操作方法。

【讨论】:

    猜你喜欢
    • 2016-05-23
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多