【问题标题】:HTTP Handler and 404 error for a custom file extension自定义文件扩展名的 HTTP 处理程序和 404 错误
【发布时间】:2012-10-06 08:57:42
【问题描述】:

关于自定义 HTTP 处理程序,我不太清楚。

我根据this blog post创建了一个ScriptTranslator HTTP Handler 我已通过以下方式在我的 web.config 文件中注册了处理程序:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
        <add name="ScriptTranslatorHandler" path="*.js.axd" verb="*" type="CamelotShiftManagement.HttpHandlers.ScriptTranslator" />
    </handlers>
</system.webServer>

我还在我的 global.asax 中添加了一个 IgnoreRoute 命令,以便网络应用可以根据 we.config 文件启动处理程序。

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

假设处理程序从我的 html 文件中翻译一个 JS 文件引用,所以我修改了我的脚本引用并在末尾添加了一个 axd 扩展名。

处理程序接收请求并搜索不带axd扩展名的文件以获取需要翻译的脚本内容,这是基本的ProccessRequest操作:

public void ProcessRequest(HttpContext context)
{
    string relativePath = context.Request.AppRelativeCurrentExecutionFilePath.Replace(".axd", string.Empty);
    string absolutePath = context.Server.MapPath(relativePath);
    string script = ReadFile(absolutePath);

    string translated = TranslateScript(script,CultureInfo.CurrentCulture);
    context.Response.Write(translated);
    Compress(context);
    SetHeadersAndCache(absolutePath, context);
}

所以在我的 html 文件中,我只修改了 script 标签的引用,没有实际的文件名为 myscript.js.axd 有一个文件名为 myscript.js

我收到 404 错误。

我对创建和使用自定义 Http 处理程序还很陌生,我不知道对使用有什么期望。

引用的blog post 意味着代码中不应该有实际的 .js.axd 文件,并且对脚本引用的请求将重新路由到处理程序并使用代码中的前 2 行处理实际的 .js 文件我之前提供的代码。

如果天气不好或者没有设置自定义 HTTP 处理程序,应该先运行处理程序代码,然后才抛出 404 错误,还是应该创建一个虚拟的 myScript.js.axd 文件来支持处理程序操作?

【问题讨论】:

    标签: c# asp.net-mvc localization httphandler


    【解决方案1】:

    忽略 url 必须如下所示:

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

    然后添加:

      <system.web>
        <httpHandlers>
          <add path="*.js.axd" verb="*" type="..." />
        </httpHandlers>
        ...
      </system.web>
    

    和:

      <system.webServer>
        ...
        <handlers>
            <add name="ScriptTranslatorHandler" path="*.js.axd" verb="*" type="..." />
        </handlers>
      </system.webServer>
    

    还要检查命名空间,文件 ScriptTranslator.cs 不包含它

    添加:

    默认routes.IgnoreRoute("{resource}.axd/{*pathInfo}");只忽略localhost/test.axd,而不是'localhost/Home/test.axd',然后应用程序尝试查找匹配的路由,但找不到它,然后我们收到404。

    【讨论】:

    • 完全按照你写的......我唯一需要改变的是传递给IgnoreRoute的值。你在{resource}之前添加了“Scripts/”,但这仍然不起作用。 ScriptTranslator.cs.. 的命名空间是什么意思。目前是CamelotShiftManagement.HttpHandlers
    • 好的!我自己找到了它..脚本本身位于/Scripts的子文件夹中...所以我将值更改为/Scripts/Administration/并presto..它可以工作..现在还有一个问题..我该怎么做Scripts 下所有子文件夹的忽略规则?
    • @Mortalus 尝试类似:routes.IgnoreRoute("Scripts/{folder}/{resource}.js.axd/{*pathInfo}");
    • 效果很好!谢谢你!所以我得到 404 的原因是 MVC 路由引擎接管了我的 js.axd 没有被 http 处理程序重新路由?您能否将其包含在您的答案中,以便我将其标记为已完成?
    • @Mortalus IgnoreRouteMapRoute 不匹配 url,然后应用程序抛出 404,如果 url 匹配 IgnoreRoute,那么应用程序没有检查 MapRoute 规则和 HttpHandler 执行.
    猜你喜欢
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 2016-03-03
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    相关资源
    最近更新 更多