在这种情况下,为什么默认路由不能使用 Html.ActionLink 工作?

发布于 2024-10-10 12:45:51 字数 1341 浏览 6 评论 0原文

我在路由方面有一个相当特殊的问题。

在一年不用担心配置之后回到路由,我使用默认路由并忽略资源路由:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
        "Default",
        // Route name
        "{controller}/{action}/{id}",

        // URL with parameters
        new
        {
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional
        });

我有一个带有 IndexRulesController code> 和 Lorem 以及 Views > 中的 Index.aspxLorem.aspx规则目录。

我在 maseter 页面上有一个针对 Rules/Index 的 ActionLink:

<li><div><%: Html.ActionLink("linkText", "Index", "Rules")%></div></li>

该链接正在呈现为 http://localhost:12345/Rules/ 并收到 404。

当我输入 Index 时 到应用程序将其路由到操作的 URL 中。

当我将默认路由操作从 "Index" 更改为 "Lorem" 时,操作链接将呈现为 http://localhost:12345/Rules/ Index 添加 Index,因为它不再位于默认路由上,并且应用程序正确路由到 Index 操作。

我使用了 Phil Haack 的路由调试器,但是输入url http://localhost:12345/Rules/ 使用它也会导致 404 错误。

我想我已经涵盖了所有菜鸟错误、相关 SO 问题和基本 RTFM。

我假设“规则”不是路由中的任何保留字。 除了更新路由和调试它们之外,我还能看什么?

I have a rather peculiar issue with routing.

Coming back to routing after not having to worry about configuration for it for a year, I am using the default route and ignore route for resources:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
        "Default",
        // Route name
        "{controller}/{action}/{id}",

        // URL with parameters
        new
        {
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional
        });

I have a RulesController with an action for Index and Lorem and a Index.aspx, Lorem.aspx in Views > Rules directory.

I have an ActionLink aimed at Rules/Index on the maseter page:

<li><div><%: Html.ActionLink("linkText", "Index", "Rules")%></div></li>

The link is being rendered as http://localhost:12345/Rules/ and am getting a 404.

When I type Index into the URL the application routes it to the action.

When I change the default route action from "Index" to "Lorem", the action link is being rendered as http://localhost:12345/Rules/Index adding the Index as it's no longer on the default route and the application routes to the Index action correctly.

I have used Phil Haack's Routing Debugger, but entering the url http://localhost:12345/Rules/ is causing a 404 using that too.

I think I've covered all of the rookie mistakes, relevant SO questions and basic RTFMs.

I'm assuming that "Rules" isn't any sort of reserved word in routing.
Other than updating the Routes and debuugging them, what can I look at?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

z祗昰~ 2024-10-17 12:45:51

确保您的网站所在目录中没有名为“Rules”的文件夹。在默认配置中,ASP.NET MVC 路由将在路由定义之前考虑物理路径。如果定义了与网站中物理文件夹的路径相匹配的路由,则将完全绕过路由引擎。

您可以通过将 RouteTable.Routes.RouteExistingFiles 属性更改为 false 来禁用到物理路径的路由,但如果您执行此操作并且您的应用程序具有到物理资源(例如图像)的路径, 、脚本、样式表等),您将需要适应那些具有匹配 IgnoreRoute() 定义的路径。
例如:RouteTable.Routes.IgnoreRoute("content/{*pathInfo}");。

Make sure there is not a folder called 'Rules' in the same directory as your website. In its default configuration, ASP.NET MVC routes will respect physical paths before route definitions. If there is a route defined which matches the path to a physical folder in the website, the routing engine will be bypassed completely.

You can disable routing to physical paths by changing the RouteTable.Routes.RouteExistingFiles property to false, but if you do this and your application has paths to physical resources (such as images, scripts, stylesheets, etc) you will need to accommodate for those paths with matching IgnoreRoute() definitions.
For example: RouteTable.Routes.IgnoreRoute("content/{*pathInfo}");.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文