Specify the application root in web.config handler mapping
I have an application with an HttpHandler that processes any requests for a .js file. I only want this handler to process *.js files that are requested in the root of the application.
The handler mapping looks like this:
<add name="HandleJS" path="*.js" verb="*" type="MyApp.JsHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
Currently, this handler processes ALL requested *.js files. Here is an example of the behavior I want.
This request would be processed by the handler:
http://localhost/myapps/approot/script.jsand this one would not be processed by the handler:
http://localhost/myapps/approot/dontProcessMe/script.jsI'd really like to avoid including the full absolute path in the handler path so I tried some other things first.
It doesn't look like the path
property of the add
element allows the use of the ~/
application root mechanism, so setting path="~/*.js"
doesn't work.
I've also tried replicating the StaticFile handler that's built into IIS and doing something like this:
<add name="MyStaticFiles" path="*/*.js" verb="*" modules="StaticFileModule" />
or
<add name="MyStaticFiles" path="dontProcessMe/*.js" verb="*"开发者_如何学运维 modules="StaticFileModule" />
Both of which just return a blank response with an HTTP status of 200.
Help me Obi Wan Kenobi, you're my only hope.
May be inspect the RequestURL in the handler itself return the actual file if there is no processing needed?
public void ProcessRequest(HttpContext context)
{
var applicationPath = context.Request.ApplicationPath;
var pathAndQuery = context.Request.Url.PathAndQuery;
var appRelativePath = pathAndQuery.Replace(applicationPath, "");
var basePath = VirtualPathUtility.GetDirectory(appRelativePath);
if (basePath != "/")
{
// load and return actual file
}
else
{
// custom logic
}
}
No dice on path="/*.js"
?
It should be working with relative paths by default...
精彩评论