簡體   English   中英

Html.RenderAction上的MVC路由映射異常:路由表中沒有路由與提供的值匹配

[英]MVC Route Map Got Exception on Html.RenderAction : No route in the route table matches the supplied values

我使用ASP.NET MVC 5,這是除Home/index之外的所有操作的“我的路線圖”:

 routes.MapRoute(
                name: "randomNumber",
                url: "{controller}/{randomNumber}/{action}",
                defaults: new { },
                constraints: new { randomNumber = @"\d+" }
            );

對於首頁: Home/Index我不想使用{randomNumber}

所以我認為的第一個解決方案是:

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

該路由圖解決了我的問題,但引起了另一個問題,即:客戶端可以訪問其他操作而無需使用{randomNumber} ,但是我只希望訪問Home Controller的Index操作時可以不使用隨機數。

另一個解決方案是:

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

但是,使用此映射,我無法使用根url訪問Home/index ,我需要使用根url訪問Home/index ,如下所示: www.mydomainaddress.com

最后我發現了這一點:

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

但是我得到一個例外: No route in the route table matches the supplied values. 在以下行的index.cshtml文件中: @{Html.RenderAction("ArchiveList", "Home");}

我不知道自己添加的路線圖和RenderAction幫助器的關系?

但是,如果我同時添加以下兩個路線圖,一切都會好的:

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

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

但是我無法添加第二條地圖路線(您需要使用隨機數訪問所有操作)

我無法理解添加Default貼圖時發生了什么,以及與RenderAction幫助程序相關的什么?

有人對此有任何想法嗎? 有什么建議嗎?

我已經在MVC5中使用了這種語法,並且可以使用:

        routes.MapRoute(
            name: "HomePage",
            url: "",
            defaults: new { controller = "Home", action = "Index" }
            );

使用此語法,您可以使用www.mydomainaddress.com訪問主頁,但如果嘗試使用www.mydomainaddress.com/home/index則無法使用

我不明白為什么它對您不起作用。 您可以嘗試重新排列順序或MapRoutes定義(將“ HomePage”路由放置在“ randomNumber”路由之后)

我看到了您修改過的問題。 我了解您希望對家庭控制器可以使用任何操作,並且不希望對HomeController使用randomNumber。

這可能更適合您(也為Home控制器禁用randomNumber路由):

        routes.MapRoute(
               name: "HomePage",
               url: "",
               defaults: new { controller = "Home", action = "Index" }
          );

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

        routes.MapRoute(
           name: "randomNumber",
           url: "{controller}/{randomNumber}/{action}",
           defaults: new { },
           constraints: new { randomNumber = @"\d+", controller = @"^((?!Home).)*$" }
       );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM