Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 当匹配不包含该参数的路由时,ActionLink将还原为querystring参数_Asp.net Mvc_Razor_Asp.net Mvc 4_Asp.net Mvc Routing - Fatal编程技术网

Asp.net mvc 当匹配不包含该参数的路由时,ActionLink将还原为querystring参数

Asp.net mvc 当匹配不包含该参数的路由时,ActionLink将还原为querystring参数,asp.net-mvc,razor,asp.net-mvc-4,asp.net-mvc-routing,Asp.net Mvc,Razor,Asp.net Mvc 4,Asp.net Mvc Routing,我正在努力实现将我的应用程序路由更改为如下的目标: <a href="/Home/5/Index">Here</a> hxxp://host/MyController/Widgets/3/AddWhatsit 此路由的视图将帮助用户向小部件3添加Whatsit 类似地,我希望创建新小部件的路径是: hxxp://host/MyController/Widgets/Create 我已经创建了单独的路线来尝试和促进这一点。它们是: routes.Map

我正在努力实现将我的应用程序路由更改为如下的目标:

<a href="/Home/5/Index">Here</a>
hxxp://host/MyController/Widgets/3/AddWhatsit

此路由的视图将帮助用户向小部件3添加Whatsit

类似地,我希望创建新小部件的路径是:

hxxp://host/MyController/Widgets/Create

我已经创建了单独的路线来尝试和促进这一点。它们是:

           routes.MapRoute("DefaultAction",
                            "{controller}/{action}",
                            new {controller = "Home", action = "Index"});

           routes.MapRoute("Default",
                            "{controller}/{id}/{action}",
                            new {controller = "Home", action = "Index", id = UrlParameter.Optional});
我遇到的问题是,当我浏览到小部件(/MyController/Widgets,匹配“DefaultAction”路由)的索引页时,任何会引入新url参数的ActionLink(不属于该路由的一部分)都会转换为querystring值。因此,例如,小部件3的编辑链接将呈现为:
Widget/Edit?id=3
而不是(我更喜欢):
Widget/3/编辑

我想我明白,我没有把我的(可选)id参数放在路线的末尾,这是在把事情搞砸


我应该把它吸起来,把id留在路线的尽头吗?

这是可能的。要使锚点链接看起来像/Home/1/Index,请设置如下路由:

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

    routes.MapRoute(
        name: "Custom",
        url: "{controller}/{id}/{action}"
        );

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

@Html.ActionLink("Here", "Index", "Home", new { id = 5 }, null)
您将得到如下所示的链接:

<a href="/Home/5/Index">Here</a>


怪癖是约束自定义路线。我删除了这种情况下的默认值,它们没有意义。当然还有路线的顺序。

我认为您需要更改路线的顺序。记住,MVC向下查看路由列表并选择第一个匹配的路由。带有ID参数的第二条路由更为具体,因此,应该放在路由表的第一位

即使在ActionLink中指定了ID参数,也指定了控制器和操作。因此,RoutingEngine正在选择第一条路线

最后,删除ID属性的可选参数。由于您希望在具有Id时选择该路由,因此您不希望该路由成为可选参数,而是希望它必须与该路由匹配

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

routes.MapRoute("DefaultAction", "{controller}/{action}", 
  new {controller = "Home", action = "Index"});

这让我非常接近。一个副作用是,如果我在视图上使用“/Widget/4/Whatsits”这样的路由,并且想要一个返回到“Widget/Index”的actionlink,那么我会得到类似“Widget/4/Index”的东西,其中4是不必要的。因此,这将从“自定义”路由跳到“默认”。看起来我的第二个问题是单独的(环境路由值),所以我将结束这个问题。谢谢你的回答。