我无法弄清楚 RouteConfig.cs 文件

I cannot figure out RouteConfig.cs file

我是 MVC 的新手,正在尝试构建我的第一个网站。我无法正确设置我的 RouteConfig 文件。我有 2 个规则适用于不同的 ActionResults。但是,只有其中一个可以正常工作。如果 GetProducts 高于 GetProductByCode,则 GetProducts 起作用。如果 GetProductByCode 高于 GetProducts,则 GetProductByCode 有效。我究竟做错了什么?

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

        routes.MapRoute(
            name: "GetProducts",
            url: "{controller}/{action}/{PageNo}",
            defaults: new { controller = "Home", action = "GetProducts", PageNo = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "GetProductByCode",
            url: "{controller}/{action}/{ProductCode}",
            defaults: new { controller = "Home", action = "GetProductByCode", ProductCode = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

我的解决方案如下

routes.MapRoute(
            name: "GetProducts",
            url: "{controller}/GetProducts/{PageNo}",
            defaults: new { controller = "Home", action = "GetProducts", PageNo = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "GetProductByCode",
            url: "{controller}/GetProductByCode/{ProductCode}",
            defaults: new { controller = "Home", action = "GetProductByCode", ProductCode = UrlParameter.Optional }
        );

asp.net 无法理解最后一个参数是 /{ProductCode}" 还是 {ProductCode}",因为操作是相同的 - 所以您的 URL 看起来相同的, 因此只进行第一个匹配,解决方案是使用完整的查询字符串,因为 /{id} 只是查询字符串中 id=5 的 shorthand

所有 3 条路线都相同,因为它们包含 3 个段(控制器名称、动作名称和一个可选参数),并且这 3 个中的第一个将始终被命中。

如果你想让GetProducts被命中你可以修改定义为

routes.MapRoute(
    name: "GetProducts",
    url: "Home/GetProducts/{PageNo}",
    defaults: new { controller = "Home", action = "GetProducts", PageNo = UrlParameter.Optional }
);

虽然好像没什么实际意义。如果您只是将 GetProducts()GetProductByCode() 中的参数名称更改为 id,那么您唯一需要的路由定义是 Default

如果你查看默认路由:

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

然后将url:之后的部分视为一种格式或模式:

  • {控制器}
  • {动作}
  • {id}

您的 3 个网址 Home/GetProductsHome/GetProductsByCodeHome/Index 都符合此模式。

{action} 部分分别为 GetProductsGetProductsByCodeIndex

如果您想将参数映射到 Action 中名为 PageNoProductCode 的变量,则需要利用路由,但通常您不需要针对每个可能组合的路由.如果您在这些操作中的参数是 id 那么它就可以正常工作而无需为每个参数创建路由。

例如

public ActionResult GetProducts(int id) 
{
 // stuff
}

public ActionResult GetProductsByCode(string id)
{
 // stuff
}

要获得参数名称,请明确指定控制器和操作:

   routes.MapRoute(
        name: "GetProducts",
        url: "Home/GetProducts/{PageNo}",
        defaults: new { controller = "Home", action = "GetProducts", PageNo = UrlParameter.Optional }
    );

    routes.MapRoute(
        name: "GetProductByCode",
        url: "Home/GetProductsByCode/{ProductCode}",
        defaults: new { controller = "Home", action = "GetProductByCode", ProductCode = UrlParameter.Optional }
    );

public ActionResult GetProducts(int PageNo) 
{
 // stuff
}

public ActionResult GetProductsByCode(string ProductCode)
{
 // stuff
}

但一般来说,只定义不同于正常 {controller}/{action}/{id} 模式的自定义路由。


MapRoute 的默认部分意味着如果找不到您的代码库中存在的 controlleraction,请改用它们。这是后备,而不是功能驱动程序。