MVC 路由映射在 Html.RenderAction 上出现异常:路由表中没有路由与提供的值匹配 [英] MVC Route Map Got Exception on Html.RenderAction : No route in the route table matches the supplied values

查看:15
本文介绍了MVC 路由映射在 Html.RenderAction 上出现异常:路由表中没有路由与提供的值匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ASP.NET MVC 5,这是除 Home/index 之外的所有操作的我的路线图:

I use ASP.NET MVC 5 and this is My Route Map for all of the actions except Home/index:

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

对于第一页:Home/Index 我不想使用 {randomNumber}

And for first page: Home/Index I don't want to use {randomNumber}

所以我认为第一个解决方案是:

So the first solution I think is:

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

这个路由图解决了我的问题,但引起了另一个问题:客户端可以在没有{randomNumber}的情况下访问其他动作,但我只想要Index的动作Home 没有随机数访问控制器.

This route map solve my problem, but caused another problem that is: clients can access to other actions without {randomNumber}, but I just want the Index action of Home Controller accessed without random number.

另一个解决方案是:

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

但是有了这张地图,我无法使用根 url 访问 Home/index,我需要使用这样的根 url 访问 Home/index:www.mydomainaddress.com

But with this map I can't access Home/index with root url, I need to access Home/index with root url like this: www.mydomainaddress.com

我终于找到了:

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

但是我得到了异常:路由表中没有与提供的值匹配的路由.index.cshtml 文件中的这一行:@{Html.RenderAction("ArchiveList", "Home");}

But I got the Exception: No route in the route table matches the supplied values. on index.cshtml file in this line: @{Html.RenderAction("ArchiveList", "Home");}

我不知道我添加的路由图和 RenderAction helper 的关系?

I don't know the ralation of the route map that I added and RenderAction helper?

但是如果我同时添加以下两个路线图,一切都会好的:

But If I add both the following two route maps, everything will be OK:

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

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

但我无法添加第二条地图路线,如您所知(我需要使用随机数访问所有操作)

But I can't add the second map route as you know (I need all actions accessed with random number)

我无法理解添加Default 贴图时发生了什么,以及与RenderAction 助手相关的内容?

I can't understand what happened when I add the Default map, and what related to RenderAction helper?

有人对此有任何想法吗?有什么建议吗?

Does any one have any idea about this? any suggestion?

推荐答案

我已经在 MVC5 中使用了这个语法并且它有效:

I have already used this syntax with MVC5 and it works :

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

此语法允许您使用 www.mydomainaddress.com 访问您的主页,但如果您尝试使用 www.mydomainaddress.com/home/index 它将不起作用>

This syntax allows you to access your home page using www.mydomainaddress.com but it won't work if you try using www.mydomainaddress.com/home/index

我不明白为什么它不适合你.您可以尝试重新排列顺序或 MapRoutes 定义(将HomePage"路由放在randomNumber"路由之后)

I don't see why it doesn't work for you. You can try rearanging the order or the MapRoutes definitions (place the "HomePage" route after the "randomNumber" route)

我看到了您修改后的问题.我了解您希望能够对主控制器使用任何操作,并且不希望能够对 HomeController 使用 randomNumber.

I saw your modified question. I understand you want to be able to use any action for the home controller and don't want to be able to use the randomNumber for the HomeController.

这可能更适合您(同时禁用 Home 控制器的 randomNumber 路由):

This probably will fit you better (also disabling the randomNumber route for Home controller) :

        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).)*$" }
       );

这篇关于MVC 路由映射在 Html.RenderAction 上出现异常:路由表中没有路由与提供的值匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆