在Html.RenderAction MVC路线图得到了例外:在路由表中没有路由提供的值相匹配 [英] MVC Route Map Got Exception on Html.RenderAction : No route in the route table matches the supplied values

查看:627
本文介绍了在Html.RenderAction MVC路线图得到了例外:在路由表中没有路由提供的值相匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC 5,这是我的路线图所有的动作,除了

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+" }
            );

和第一页:我不想用 {randomNumber}

因此​​,第一个解决方案,我认为是:

So the first solution I think is:

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

该路线图解决我的问题,但造成的另一个问题是:客户端可以访问其他操作,而不 {randomNumber} ,但我只是想在首页 首页控制器不随机数进行访问。

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,我需要访问与根URL是这样的: 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中,家);}

我不知道的路线图,我添加的关系Cyclobutyl和的RenderAction 帮手?

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)

我不明白发生了什么事,当我添加默认地图,什么相关的的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 /家庭/指数

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

我不明白为什么它不为你工作。您可以尝试rearanging的顺序或MapRoutes定义(请将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)

我看到了你修改的问题。我理解你希望能够使用的家居控制器的任何行动,不希望能够使用randomNumber为HomeController的。

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.

这可能会适合你更好的(也禁止用于家庭控制器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).)*$" }
       );

这篇关于在Html.RenderAction MVC路线图得到了例外:在路由表中没有路由提供的值相匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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