c# - Can I use a generic handler to process requests differently based on url path? -


say example have generic handler - ~/handler.ashx - processes requests url via rewrite, https://www.somedomain.com/foo/bar , spits out xml response in 1 format, can have same handler respond requests /bar/foo in xml format?

yes. generic handler absolutely free generate whatever response wants. hint in name: "handler" (as opposed "filter" or "module", in ihttpmodule vs ihttphandler).

you need access , parse original url yourself, of course, generic handlers predate asp.net routing. can via http_x_rewrite_url server variable (i believe request.rawurl contains it, don't know if guaranteed).

static readonly regex _routex = new regex("foo/(.+)", regexoptions.compiled ); static readonly regex _routey = new regex("baz/(.+)"/qux, regexoptions.compiled );  public void handlerequest(httpcontext context) {     if( _routex.ismatch( context.request.rawurl ) )     {         context.response.write("<xml><myobject>foo</myobject></xml>");     }     else if( _routey.ismatch( context.request.rawurl ) )     {         context.response.write("<anotherxmlfile>baz</anotherxmlfile>");     } } 

that said - bad design because handler responsible 2 different business tasks. there reason can't separate handlers - or asp.net mvc or asp.net web api?


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -