ASP.Net MVC 2 RC2:当所有可选参数均为 null 时,自定义路由返回 404

发布于 2024-08-22 09:03:04 字数 1841 浏览 6 评论 0原文

当我使用以下路由导航到以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧时浪漫 2024-08-29 09:03:04

您还可以尝试以下操作(因为您使用的是 MVC 1.0)。

在当前路由之上添加一个路由:

routes.MapRoute(
    "Properties", "Properties", new { controller = "Properties", action = "List"} 
);

并向控制器添加一个重载的 ActionResult List() 方法:

public ActionResult List()
{
     return View();
}

You can also try the following (since you're using MVC 1.0).

Add a route above the current route:

routes.MapRoute(
    "Properties", "Properties", new { controller = "Properties", action = "List"} 
);

And add an overloaded ActionResult List() method to your controller:

public ActionResult List()
{
     return View();
}
北方。的韩爷 2024-08-29 09:03:04

您是否尝试过 Phil Haack 的路由调试器?它可以帮助您确定发生了什么。

Have you tried this Route Debugger from Phil Haack? It may help you determine what is going on.

吹泡泡o 2024-08-29 09:03:04

不要通过

(string)null

传递

UrlParameter.Optional

尝试按照 Phil Haacks 帖子 此处。我不知道这是否能解决问题,因为我目前无法测试它。

Instead of passing

(string)null

try passing

UrlParameter.Optional

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.

萌逼全场 2024-08-29 09:03:04

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."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文