asp.net - Is it possible to select a custom handler by request verb? -
i write custom handler handle requests options verb.
public class optionsrequesthandler : ihttphandler { public void processrequest(httpcontext context) { string origin = context.request.headers.get("origin"); context.response.addheader("access-control-allow-origin", origin); context.response.addheader("access-control-allow-methods", "*"); context.response.addheader("access-control-allow-headers", "accept, authorization, content-type"); } public bool isreusable { { return false; } } } and have registered handler in web.config.
<system.webserver> <modules> ...... </modules> <handlers> ...... <add name="optionshandler" path="*" verb="options" type="reams.infrastructure.requesthandlers.optionsrequesthandler"/> <add name="extensionlessurlhandler-integrated-4.0" path="*." verb="get,post,delete,put,head" type="system.web.handlers.transferrequesthandler" precondition="integratedmode,runtimeversionv4.0" /> </handlers> but handler never selected options requests. there wrong? thanks!
finally figured out problem. because, default, mvc framework map request handler path, not possible map handler request request verb.
to this, need preempt handler selection of mvc , implement own module. here working copy interested.
public class optionsverbmodule : ihttpmodule { public void init(httpapplication context) { context.postrequesthandlerexecute += onpostrequesthandlerexecute; context.postresolverequestcache += onpostresolverequestcache; } private void onpostresolverequestcache(object sender, eventargs eventargs) { if (string.equals(httpcontext.current.request.httpmethod, "options", stringcomparison.ordinalignorecase)) { httpcontext.current.remaphandler(new optionsrequesthandler()); } } private void onpostrequesthandlerexecute(object sender, eventargs e) { string origin = httpcontext.current.request.headers.get("origin"); if (origin != null) { httpcontext.current.response.addheader("access-control-allow-origin", origin); httpcontext.current.response.addheader("access-control-allow-credentials", "true"); } } public void dispose() { } } what module check verb of request , if of 'options' request, selected customized handler in question rather map mvc handler.
i because find existing enablecors web api not suitable application needs , using customized process have more control well.
Comments
Post a Comment