asp.net-mvc – MVC 3应用程序中的自定义Http处理程序

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – MVC 3应用程序中的自定义Http处理程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Http Handler来本地化我的应用程序中使用的 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添加Handler时会找到该文件但实际上并没有找到该文件,因为该文件名为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处理程序.

解决方法

好的…所以我确实修好了(我想).

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

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

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

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

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

如果有人有其他见解,一定要做.

原文链接:https://www.f2er.com/aspnet/709528.html

猜你在找的asp.Net相关文章