自定义HTTP处理在MVC 3应用 [英] Custom Http Handler in MVC 3 Application
问题描述
我使用HTTP处理程序本地化在我的应用程序使用的JavaScript文件:
请参阅:在ASP.NET 在JavaScript文件本地化文本一>
I'm using an Http Handler to localize javascript files used in my application: see: Localize text in JavaScript files in ASP.NET
我想使用提供这样的处理,我做了以下内容:
I want to use the handler provided so I did the following:
1)在使用本code在Global.asax中忽略路线 - 我已经添加了 routes.IgnoreRoute({}资源.js.axd / {*} PATHINFO);
code到的RegisterRoutes
方法的行,所以它看起来是这样的:
1) Ignored routes using this code in Global.asax - I have added the routes.IgnoreRoute("{resource}.js.axd/{*pathInfo}");
line of code to the RegisterRoutes
method so it looks like this:
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)我已经加入<添加路径=* js.axd。动词=*TYPE =CamelotShiftManagement.HttpHandlers.ScriptTranslator/>
线在浏览文件夹我的web.confing文件,所以它看起来是这样的:
2) I have added <add path="*.js.axd" verb="*" type="CamelotShiftManagement.HttpHandlers.ScriptTranslator" />
line to my web.confing file in the Views folder so it looks like this:
<system.web>
<httpHandlers>
<add path="*.js.axd" verb="*" type="CamelotShiftManagement.HttpHandlers.ScriptTranslator" />
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
然而,我得到的找不到网页的错误,当我尝试访问以下网址:
And yet I get a Page not found error when I try to access the following URL:
http://camelotshiftmanagement.com/Scripts/Administration/OrganizationalStructure.js.axd
我在做什么错在这里?
What am I doing wrong here ?
进展:
好吧,我发现了一个尴尬的错误,我做...
出于某种原因,我认为这增加了对
*。js.axd
会发现文件,但实际上它并没有因为该文件被命名为 OrganizationalStructure处理程序时.js文件
不用其他的个.axd
扩展。所以这是对的 404错误的原因,但现在我从服务器获取不同的错误,我又需要你的帮助。
Progress: Okay, so I found out an awkward mistake i made... For some reason I thought that when adding a Handler for
*.js.axd
will find the file but actually it did not because the file was named OrganizationalStructure.js
witout the .axd
extension.
So that is the reason for the 404 error but now i get a different error from the server and I need your help again.
访问 http://camelotshiftmanagement.com/Scripts/Administration/OrganizationalStructure.js.axd
产生了不同的错误这个时候: 404.17请求的内容似乎是脚本并不会由静态文件处理程序送达。的
accessing http://camelotshiftmanagement.com/Scripts/Administration/OrganizationalStructure.js.axd
produced a different error this time: 404.17 The requested content appears to be script and will not be served by the static file handler.
其他错误信息
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 的处理程序被调用。
推荐答案
好吧......所以我做修复它(我认为)。
Okay... so I did fix it (I think).
有两个问题:
1.文件名中有一个的.js
延伸,而不是 .js.axd
的处理程序需要。
2.我需要的,因为它是一个自定义扩展,默认情况下不recoginzed注册处理程序到IIS。
要做到这一点,我添加以下code下&LT; system.webServer&GT;
在主 Web.Config中的节点
我的MVC应用程序的文件:
There were two problems:
1. the file name had a .js
extention and not .js.axd
as the handler needs.
2. I needed to register the handler to the IIS since it is a custom extension that is not recoginzed by default.
To do that I added the following code under <system.webServer>
node at the Main Web.Config
file of my MVC application:
<handlers>
<add name="CustomScriptHandler" path="*.js.axd" verb="*" type="CamelotShiftManagement.HttpHandlers.ScriptTranslator" />
</handlers>
的此外,还有可以使用IIS管理器来完成一个GUI程序(7):
打开网站节点 - >处理程序映射 - >添加脚本映射的
没有正确的处理程序由服务器和code运行解雇了。
No the correct handler is fired by the server and the code runs.
我不知道的唯一的事情是,我仍然必须有与 .js.axd
延伸和的.js文件
扩展becuse处理程序查找JavaScript文件处理和服务器寻找一个 .js.axd
文件来启动自定义的处理程序。
The only thing I am not sure of is that i still have to have a file with an .js.axd
extention and a .js
extension becuse the handler looks for the Javascript file to process and the server looks for a .js.axd
file to start the custom handler.
如果任何人有其他的见解,通过各种手段做的。
If anyone have other insights, by all means do.
这篇关于自定义HTTP处理在MVC 3应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!