Custom Http Handler in MVC 3 Application

2019-05-07 01:22发布

问题:

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) 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) 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 ?


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.

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.

Additional error information

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.

Well I am way over my league here... I do not understand why my custom handler is not called and instead a StaticFile handler is called.

回答1:

Okay... so I did fix it (I think).

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>

There is also a GUI process that can be done using the IIS Manager (7): Open website node --> Handler Mapping --> Add Script Map

No the correct handler is fired by the server and the code runs.

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.