【发布时间】:2017-01-29 05:13:55
【问题描述】:
我正在尝试在 RouteConfig.cs 文件中配置自定义 url,如下所示:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default2",
url: "{controller}/{action}/id/{id}", //Custom url route
defaults: new { controller = "Product", action = "AddImages", id = UrlParameter.Optional }
);
}
当我在 url 中写下以下内容时,上述工作正常:1st routing
http://localhost:1234/Product/AddImages/id/1008
同样,以下也适用:第二次路由
http://localhost:1234/Product/AddImages/1008
但我想强制 url 在地址栏中显示或写入 url 作为特定页面“AddImages”的第一个,如下所示:http://localhost:1234/Product/AddImages/id/1008
有什么办法可以做到,我的意思是保持默认路由,对于特定页面,第一个路由选项?
我再次尝试从第一个 url 中检索 QueryString,如下所示:
int id = Convert.ToInt32(Url.RequestContext.RouteData.Values["id"]); //This works for the second routing option
它说 - 输入字符串的格式不正确。我猜,这是默认路由选项,因为它被配置为:
url: "{controller}/{action}/{id}"
再次,是否可以从以下 url 获取 QueryString 值或进行任何配置:
http://localhost:1234/Product/AddImages/id/1008
【问题讨论】:
-
您的路线顺序错误 -
Default2必须排在第一位(Default之前) -
@Stephen Muecke 我能够通过订购路由来解决。感谢您和无脑程序员的想法。
标签: c# asp.net-mvc url routes