An invalid httpHandlers configuration solution for the Linux ASP. net mvc website
Linux ASP. NET users, the Web. the httpHandlers configuration added to config is used to process custom types, but the expected results are not displayed during running. The server returns the 404 error (the webpage is not found. I personally tested that the httpHandlers node configuration is valid on the WebForm website, but does not work in MVC. If this problem cannot be solved, it will seriously affect the deployment of Linux ASP. NET and the compatibility and integrity of Windows ASP. NET migration to Linux.
I don't have time to go into the cause of the invalid httpHandlers. In order to solve this problem in time, I put my attention to the Application_BeginRequest METHOD OF THE Global. asax file and then gave the following solution.
1. Add a static method to global. asax:
Static bool TryHanler <T> (string ext) where T: IHttpHandler
{
If (string. IsNullOrEmpty (ext) return false;
Var context = HttpContext. Current;
Var path = context. Request. AppRelativeCurrentExecutionFilePath;
If (! Path. EndsWith (ext) return false;
Var handle = Activator. CreateInstance (typeof (T) as IHttpHandler;
If (handle = null) return false;
Handle. ProcessRequest (context );
Context. Response. End ();
Return true;
}
Note: This is a generic method. T represents the custom class that you use to process a path inherited from IHttpHandler, the ext parameter is the extension of the Request Path (including ". ).
2. Implement the Application_BeginRequest method in global. asax and call TryHandler in this method. For example:
Protected void Application_BeginRequest (object sender, EventArgs e)
{
If (TryHandler <myHandler> (". do") return;
}
Note: This solution is universal and compatible with Windows IIS and Linux Jexus or XSP.
This article permanently updates the link address: