MVC 3路由和行动不链接下列预期上下文路径 [英] MVC 3 Routing and Action Links not following expected contextual Route

查看:154
本文介绍了MVC 3路由和行动不链接下列预期上下文路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个定制的路线,所以我可以preFIX一个网址在我的应用程序有选择的串并以此为基础进行一些处理。我快到的问题是,所生成的链接行动的基础上,它存在于URL不语境。

I'm attempting to do a custom route so I can prefix a url in my application with a chosen string and the do some processing based on that. The problem I'm running into is, that the action links that are generated are not contextualized based on the url that it exists on.

路线:

routes.MapRoute(
            "TestRoute",
            "TEST/{controller}/{action}/{id}",
            new { controller = "Space", action = "Index", id = UrlParameter.Optional });

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

导航到TEST /空间/索引的工作原理,以及空间/指数,但奇怪的问题,我需要固定是通过ActionLink的生成的链接不服从它们所加载的情况下,至少在{控制器} / {行动} / {ID}缺省路由。那些在测试/空间/索引列表链接正确加载页面,但在加载/空间/索引时,它们都引用TEST /路由呼叫URL不。这是默认的行为?有没有办法让这些链接在适当的情况下产生?

Navigating to TEST/Space/Index works, as well as Space/Index, but the odd issue I need fixed is that the links generated via ActionLink do not obey the context in which they are loaded, at least for the {controller}/{action}/{id} default route. Pages that are loaded under TEST/Space/Index list links properly, but when /Space/Index is loaded, they are all referencing the TEST/ route that the calling url does not. Is this the default behavior? Is there a way to get these links to generate in the proper context?

编辑:

我看到这的第一个地方是没有Html.BeginForm测试/

The first place I saw this was in the Html.BeginForm without the TEST/

Html.BeginForm("ToTheMoon", "Space", FormMethod.Post)

这使得该链接为TEST /空间/ ToTheMoon

which renders the link as TEST/Space/ToTheMoon

但它也显示在链接:

@Html.ActionLink("Take Me To The Space Port", "SpacePort", "Space")

这使得测试/空间/太空港

which renders TEST/Space/SpacePort

推荐答案

我发现了一点解决的办法,这样的背景下不会丢失。这是我获得这项工作。

I found a bit of a way around this so that the context wouldn't be lost. Here's what I did to get this to work.

TestRoute改变这样的:

TestRoute changes to this:

routes.MapRoute(
    "TestRoute",
    "{path}/{controller}/{action}/{id}",
    new { controller = "Space", action = "Index", id = UrlParameter.Optional },
    new { path = @"TEST" },
    new string[] { "The.Namespace" });

设置路径上的限制,并从路线移除它使该路由的工作。现在我可以打到/测试/空间/指数和ActionLink的生成表现为预期的我所有的链接。同样在一个相关的问题,我结束了在添加地图路线命名规范,作为开发环境要求在有正确路线的东西到测试路径。

Setting the constraint on path and removing it from the route makes this routing work. Now I can hit the /TEST/Space/Index and all my links generated from ActionLink behave as intended. Also on a related issue, I ended up adding the namespace specification in the map route, as the development environment required that be in there to properly route things to the TEST path.

一些我找到了信息的是的在这个页面

Some of the info I found was on this page.

这篇关于MVC 3路由和行动不链接下列预期上下文路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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