ASP.Net MVC 2 RC2:当所有可选参数均为 null 时,自定义路由返回 404
当我使用以下路由导航到以下 URL 时,出现 404 错误:
http://localhost:53999/properties/
但是,以下所有内容都正确路由到我的控制器中的 List 操作:
http://localhost:53999/properties/usa/new-york/manhattan/12
http://localhost:53999/properties/usa/new-york/manhattan
http://localhost:53999/properties/usa/new-york
http://localhost:53999/properties/usa
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//properties
routes.MapRoute(
"Properties",
"Properties/{country}/{state}/{city}/{id}",
new
{
controller = "Properties",
action = "List",
country = UrlParameter.Optional,
state = UrlParameter.Optional,
city = UrlParameter.Optional,
id = UrlParameter.Optional
}
);
//default
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
在 PropertiesController.cs 中:
public ActionResult List(string country, string state, string city, string id)
{
return View();
}
有人知道我缺少什么吗?看起来它应该只是进入默认操作,但显然不是......
I get a 404 error when I navigate to the following URL using the route below:
http://localhost:53999/properties/
However, all the following are correctly routed to the List action in my controller:
http://localhost:53999/properties/usa/new-york/manhattan/12
http://localhost:53999/properties/usa/new-york/manhattan
http://localhost:53999/properties/usa/new-york
http://localhost:53999/properties/usa
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//properties
routes.MapRoute(
"Properties",
"Properties/{country}/{state}/{city}/{id}",
new
{
controller = "Properties",
action = "List",
country = UrlParameter.Optional,
state = UrlParameter.Optional,
city = UrlParameter.Optional,
id = UrlParameter.Optional
}
);
//default
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
In PropertiesController.cs:
public ActionResult List(string country, string state, string city, string id)
{
return View();
}
Anyone know what I'm missing? Looks like it should just go to the default action, but it obviously doesn't...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您还可以尝试以下操作(因为您使用的是 MVC 1.0)。
在当前路由之上添加一个路由:
并向控制器添加一个重载的
ActionResult List()
方法:You can also try the following (since you're using MVC 1.0).
Add a route above the current route:
And add an overloaded
ActionResult List()
method to your controller:您是否尝试过 Phil Haack 的路由调试器?它可以帮助您确定发生了什么。
Have you tried this Route Debugger from Phil Haack? It may help you determine what is going on.
不要通过
传递
尝试按照 Phil Haacks 帖子 此处。我不知道这是否能解决问题,因为我目前无法测试它。
Instead of passing
try passing
as specified in Phil Haacks post here. I don't know if this will solve the issue as I'm not currently in a position to test it.
Brad Wilson 在这篇文章中回答了这个问题: http://forums.asp.net/ p/1527697/3690295.aspx#3690295
“不,问题是您的磁盘上有一个 Properties 文件夹,因此它通过标准调度程序(而不是 MVC)进行调度,然后由于找不到而出现 404ing默认文档。”
Brad Wilson answered it on this post: http://forums.asp.net/p/1527697/3690295.aspx#3690295
"No, the problem is that you have a Properties folder on your disk, so it's dispatching through the standard dispatcher (not MVC), and then 404ing because it can't find a default document."