Custom Http Handler in MVC 3 Application

I use an Http handler to localize the javascript files used in my application: see Localize Text in JavaScript Files in ASP.NET

I want to use a handler, so I did the following:

1) Ignored routes using this code in Global.asax - I added the lines of code routes.IgnoreRoute("{resource}.js.axd/{*pathInfo}"); in the RegisterRoutes method so that 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 added the line <add path="*.js.axd" verb="*" type="CamelotShiftManagement.HttpHandlers.ScriptTranslator" /> to my web.confing file in the Views folder so that 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 still, I get a page error message when I try to access the following URL:

 http://camelotshiftmanagement.com/Scripts/Administration/OrganizationalStructure.js.axd 

What am I doing wrong here?

<h / "> Progress: Okay, so I found the awkward mistake I made ... For some reason, I thought that when adding Handler for *.js.axd would find the file, but in fact it is not, because the file was named OrganizationalStructure.js without the .axd extension, so this is the cause of the 404 error, but now I get another error from the server and I need your help again.

Access to http://camelotshiftmanagement.com/Scripts/Administration/OrganizationalStructure.js.axd this time caused another error: 404.17 The requested content looks like a script and will not be served by a 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'm over my league here ... I donโ€™t understand why my custom handler is not called, but the StaticFile handler is called instead.

+4
source share
1 answer

Good ... so I fix it (think).

There were two problems: 1. The file name had the extension .js , not .js.axd , as required by the handler. 2. I had to register the handler in IIS, since this is a custom extension that is not recognized by default. To do this, I added the following code to the <system.webServer> node in 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 performed using the IIS Manager (7): Open the node website โ†’ Mapping handlers โ†’ Add Script Map

There is no proper handler that the server starts, and the code starts.

The only thing I'm not sure about is that I still need to have a file with the .js.axd extension and the .js extension, because the handler is looking for a Javascript file for processing, and the server is looking for .js.axd to start the custom handler.

If anyone has other ideas, be sure.

+1
source

Source: https://habr.com/ru/post/1437855/


All Articles