1

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

ScriptTranslator根据这篇博客文章创建了一个 HTTP 处理 程序,我已经通过以下方式在我的 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>

我还IgnoreRoute向我的 global.asax 添加了一个命令,以便网络应用程序可以根据 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 处理程序还很陌生,我不知道对使用有什么期望。

引用的博客文章暗示代码中不应该有实际的 .js.axd 文件,并且对脚本引用的请求将重新路由到处理程序并使用我之前提供的代码中的前两行来处理实际的 .js 文件那。

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

4

1 回答 1

3

忽略 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。

于 2012-10-06T10:48:20.643 回答