我有一个本地化 httphandler,它在 ASP.Net MVC2 Content 文件夹的上下文中运行(它所做的一部分是编译 /Content/css 中的 .less 文件)。我对这组特定请求的默认路由如下所示:

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

context.MapRoute(
    "Area_default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new { controller = new VirtualDirectoryConstraint("VDirectory1") },
    new string[] { "Namespace.One.MVC" }
);

(顺便说一句 - 我认为它不相关,但以防万一 - 如果请求不是来自传入的应用程序路径/虚拟目录,则 VirtualDirectoryConstraint 会拒绝此路由上的匹配)

通过此配置,调用 http://server.net/VDirectory1/Content/css/LessCompiler.axd 失败,因为没有 ContentController 类。一切都很好。

当我添加

context.Routes.IgnoreRoute("{Content}/{*pathInfo}");

该调用成功,但后续调用

http://server.net/VDirectory1/Localization/ClientScript/en-US.js

http://server.net/VDirectory1/Authorization/ClientScript

失败。查看 Phil Haack 的 RouteDebugger 工具,这些调用与 Content IgnoreRoute 路由匹配:

True    {Content}/{*pathInfo}   (null)  (null)  (null)

因此不会分别路由到 LocalizationController 和 AuthorizationController。

显然,我误解了如何使用 IgnoreRoute 以及为什么特定的 IgnoreRoute 与这些请求匹配。我缺少什么?

有帮助吗?

解决方案

你的 IgnoreRoute 不应该使用 Content 代替 {Content} ?

context.Routes.IgnoreRoute("Content/{*pathInfo}");

眼下, {Content} 可能被扩展为一个变量,什么都没有,这使得路径信息匹配所有内容。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top