【问题标题】:MVC3 Url Routing issueMVC3 URL路由问题
【发布时间】:2013-02-16 10:48:11
【问题描述】:

我正在尝试按如下方式注册路线:

routes.MapRoute(
            "SaleReport", // Route name
            "SaleReport/GetDataConsolidated/{type}",
            new { controller = "SaleReport", 
                  action = "GetDataConsolidated",
                  type =   UrlParameter.Optional});

在控制器中

public ActionResult GetDataConsolidated(string type)

    {
      return Content("Report Type = " + type);
    }

我这样称呼它:localhost:56674/SaleReport/GetDataConsolidated/Sale

但问题是类型的值始终为空。 我做错了什么?

【问题讨论】:

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


    【解决方案1】:

    这可能只是.MapRoute(...) 调用的顺序。

    将您的“SaleReport”.MapRoute(...) 调用放在“默认”{controller}/{action}.MapRoute(...) 调用之前,因为它更具体。

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapRoute(
            name: "SaleReport",
            url: "SaleReport/GetDataConsolidated/{type}",
            defaults: new { controller = "SaleReport", action = "GetDataConsolidated", type = UrlParameter.Optional });
    
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
    

    【讨论】:

    • 我已经这样做了。默认路线是在销售报告后定义的。
    • 我刚刚在本地重新创建了整个示例,它工作正常。在“SalesReport”之前是否有其他路线?您没有意外更改变量名(类型)?到目前为止,您在示例中描述的内容有效。
    • 感谢您的努力,但我没有改变任何东西。一样的
    • 检查控制器是否准确命名为“SaleReportController”
    【解决方案2】:

    是否有特殊需要定义另一条地图路线?

    它应该与默认路由一起使用,

    routes.MapRoute(
            "SaleReport", // Route name
            "SaleReport/GetDataConsolidated/{type}",
            new { controller = "SaleReport", 
                  action = "GetDataConsolidated",
                  type =   UrlParameter.Optional});
    

    删除以上路线, 只需更改如下操作方法

    public ActionResult GetDataConsolidated(string id)
    
    {
      return Content("Report Type = " + id);
    }
    

    这会起作用的,谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 2012-02-19
      相关资源
      最近更新 更多