如何修复404与路由在ASP.净MVC

本文关键字:ASP MVC 路由 何修复 | 更新日期: 2023-09-27 18:19:25

我有一个问题,试图得到路由与ASP工作。Net MVC 3.0。我声明了以下路由:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
    new { controller = "Home", action = "RsvpForm", id = UrlParameter.Optional } 
    );
    routes.MapRoute(
        "TestRoute",
        "{id}",
        new { controller = "Product", action = "Index3", id = UrlParameter.Optional }
    );
    routes.MapRoute(
        "TestRoute2",
        "{action}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

当我访问:

http://localhost

站点工作正常,似乎到达了Default路由。

当我访问:

http://localhost/1

我得到404:

'/'应用程序服务器错误。

资源找不到。描述:HTTP 404。您正在查找的资源(或其依赖项之一)可能已被删除,或其名称已更改暂时不可用。请查看以下网址并制作确保拼写正确。

请求的URL:/1

下面是这些路由对应的动作:

public ActionResult Index3(int? id)
{
    Product myProduct = new Product
    {
        ProductID = 1,
        Name = "Product 1 - Index 3",
        Description = "A boat for one person",
        Category = "Watersports",
        Price = 275M
    };
    Product myProduct2 = new Product
    {
        ProductID = 2,
        Name = "Product 2 - Index 3",
        Description = "A boat for one person",
        Category = "Watersports",
        Price = 275M
    };
    ViewBag.ProcessingTime = DateTime.Now.ToShortTimeString();
    if (id == 1)
        return View("index", myProduct);
    else
        return View("index", myProduct2);
}

我如何构建我的路由,使所有三个动作方法都正确命中?

如何修复404与路由在ASP.净MVC

. NET MVC路由从上到下评估路由。因此,如果两个路由匹配,它命中的第一个(靠近RegisterRoutes方法的"顶部"的那个)将优先于后面的一个。

考虑到这一点,您需要做两件事来解决问题:

  1. 你的默认路由应该在底部。
  2. 你的路由需要有约束,如果他们包含相同数量的段:

example.com/1

example.com/index

对于解析器来说,它们包含相同数量的段,并且没有微分器,所以它将击中列表中匹配的第一条路由。

要解决这个问题,您应该确保使用ProductIds的路由接受约束:

routes.MapRoute(
    "TestRoute",
    "{id}",
    new { controller = "Product", action = "Index3", id = UrlParameter.Optional },
    new { id = @"'d+" } //one or more digits only, no alphabetical characters
);

你的设置还有其他问题,但这是我马上想到的两件事。

你的路由MapRoute Default应该是最后一个。

routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
    new { controller = "Home", action = "RsvpForm", id = UrlParameter.Optional } 
    );

将最通用的路由推送到MapRoute调用链的最后。

试试这个:

public static void RegisterRoutes(RouteCollection routes)
{
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
                "TestRoute",
                "{id}",
                new { controller = "Product", action = "Index3", id = UrlParameter.Optional }
        );
        routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "RsvpForm", id = UrlParameter.Optional } // Parameter defaults
                //new { controller = "Product", action = "Index2", id = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
                "TestRoute2",
                "{action}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
}

将默认路由移到最后,默认路由应该是最后定义的路由,因为它的作用是捕获所有路由

在我的例子中,同样问题的答案是需要"在项目中包含"相关的控制器和视图,而不是错误的路由规则。

在创建mine时,由于某些原因没有自动包含它们。这个问题是在我关闭和重新打开解决方案后发现的。

{+1恨}授予Visual Studio,因为它有缺陷的超自动化让我在网络上挖掘。配置文件,尝试添加扩展,甚至尝试(和失败)加快一个像样的ErrorController。