4

我正在使用 Http 处理程序来本地化我的应用程序中使用的 javascript 文件:请参阅:Localize text in JavaScript files in ASP.NET

我想使用提供的处理程序,所以我做了以下事情:

1) 在 Global.asax 中使用此代码忽略路由 - 我已将routes.IgnoreRoute("{resource}.js.axd/{*pathInfo}");代码行添加到RegisterRoutes方法中,因此它看起来像这样:


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

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
         new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }

2) 我<add path="*.js.axd" verb="*" type="CamelotShiftManagement.HttpHandlers.ScriptTranslator" />在 Views 文件夹中的 web.confing 文件中添加了一行,如下所示:


<system.web>
   <httpHandlers>
      <add path="*.js.axd" verb="*"  type="CamelotShiftManagement.HttpHandlers.ScriptTranslator" />
      <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
   </httpHandlers>

然而,当我尝试访问以下 URL 时,我得到一个Page not found错误:

http://camelotshiftmanagement.com/Scripts/Administration/OrganizationalStructure.js.axd

我在这里做错了什么?


进度: 好的,所以我发现了我犯的一个尴尬的错误......出于某种原因,我认为添加处理程序时*.js.axd会找到文件,但实际上并没有,因为文件被命名为OrganizationalStructure.js没有.axd扩展名。这就是404 错误的原因,但现在我从服务器收到一个不同的错误,我再次需要你的帮助。

http://camelotshiftmanagement.com/Scripts/Administration/OrganizationalStructure.js.axd这次访问产生了不同的错误: 404.17 请求的内容似乎是脚本,不会由静态文件处理程序提供服务。

附加错误信息

Server Error in Application "CAMELOTSHIFTMANAGEMENT.COM"
Internet Information Services 7.5
Error Summary
HTTP Error 404.17 - Not Found
The requested content appears to be script and will not be served by the static file handler.

Detailed Error Information
Module:  "StaticFileModule"
Notification:  "ExecuteRequestHandler"
Handler:  "StaticFile"
Error Code:  "0x80070032"
Requested URL:  "http://camelotshiftmanagement.com:80/Scripts/Administration/OrganizationalStructure.js.axd"
Physical Path:  "C:\Code\CamelotShiftManagement\CamelotShiftManagement\Scripts\Administration\OrganizationalStructure.js.axd"
Logon Method:  "Anonymous"
Logon User:  "Anonymous"

Most likely causes:
The request matched a wildcard mime map. The request is mapped to the static file handler. If there were different pre-conditions, the request will map to a different handler.

Things you can try:
If you want to serve this content as a static file, add an explicit MIME map.

好吧,我在这里已经超越了我的联盟......我不明白为什么我的自定义处理程序没有被调用,而是调用了一个StaticFile处理程序。

4

1 回答 1

1

好的...所以我确实修复了它(我认为)。

有两个问题:1.文件名有.js扩展名,而不是.js.axd处理程序需要的。2. 我需要将处理程序注册到 IIS,因为它是默认情况下无法识别的自定义扩展。为此,我在MVC 应用程序<system.webServer>的主文件的节点下添加了以下代码:Web.Config

<handlers>
        <add name="CustomScriptHandler" path="*.js.axd" verb="*" type="CamelotShiftManagement.HttpHandlers.ScriptTranslator" />
</handlers>

还有一个GUI过程可以使用IIS管理器(7):打开网站节点-->处理程序映射-->添加脚本映射

没有正确的处理程序被服务器触发并且代码运行。

我唯一不确定的是我仍然必须有一个带有.js.axd扩展名和.js扩展名的文件,因为处理程序会查找要处理的 Javascript 文件,而服务器会查找.js.axd文件以启动自定义处理程序。

如果有人有其他见解,请务必这样做。

于 2012-10-04T22:24:50.407 回答