【问题标题】:URL Routing in MVC 3MVC 3 中的 URL 路由
【发布时间】:2011-08-30 23:51:36
【问题描述】:

我当前的网址是这样的 => http://localhost:4330/Restaurants/?Location=Manchester&Cuisine=0&NetProfit=0&Turnover=0&MaxPrice=120000&SortPriceBy=Low&Page=0

我希望它做出这样的东西 => http://localhost:4330/Restaurants/Manchester/?Cuisine=Chinese&MaxPrice=120000

其中不包含值 (0) 的 Param 查询字符串不会包含在查询字符串 URL 中 有可能吗?

【问题讨论】:

  • 如果你想隐藏某些参数不显示为查询字符串,那么你可以通过其他方法传递它们,即表单值。

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


【解决方案1】:

更新

string将此添加到 Global.asax 路由

            routes.MapRoute(
                "Name of route", // Route name
                "Restaurants/{cityid}/", // URL with parameters
                new { controller = "Restaurants", action = "Index" } // Parameter defaults
            );

这是控制器:

public ActionResult Index(string city, int cuisine = 0, int ChineseMaxPrice=0)
{
  Return View();
}

Like int Cuisine = 0 - 如果查询字符串中未设置此参数,则将默认值设置为参数

字符串城市 - 是一个应该在字符串中的参数(不是可选的)

【讨论】:

  • 那条路由不是双通配符吗?它匹配任何有任何动作的控制器..?或者是 new 子句在其中指定特定的控制器和操作?
  • 对不起,我犯了一个小错误/更新了。这是附加路线
【解决方案2】:

尝试添加添加对应的路由:

routes.MapRoute(
    "Restaurants",
    "Restaurants/{city}",
    new { controller = "Restaurants", action = "Index", city = UrlParameter.Optional }
);

这将映射到Restaurants 控制器上的Index 操作:

public ActionResult Index(string city) 
{
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    相关资源
    最近更新 更多