[英]HTTP Handler and 404 error for a custom file extension
对于自定义HTTP处理程序,我有些不太清楚的事情。
我已根据此博客文章创建了一个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
命令,因此web-app可以根据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文件中我只修改了脚本标签的引用,没有名为myscript.js.axd
实际文件,有一个名为myscript.js
的文件。
我收到404错误。
我很擅长创建和使用自定义Http Handler,我不知道该用法会带来什么。
引用的博客文章暗示代码中应该没有实际的.js.axd文件,并且脚本引用的请求将重新路由到处理程序并使用我之前提供的代码中的前两行处理实际的.js文件那。
我是否更天气设置自定义HTTP处理程序应首先运行处理程序代码然后再抛出404错误,还是应该创建一个虚拟myScript.js.axd文件以支持处理程序操作?
忽略网址必须如下所示:
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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.