asp.net - In ASP MVC3, how can execute a controller and action using a uri? -


how can i, when executing controller action, take uri (not 1 requested) , invoke action controller have been executed had uri been 1 called? can't redirect action need happen in same request context.

assuming have access httpcontext (and suppose since asking) could:

var routedata = new routedata(); // controller , action compulsory routedata.values["action"] = "index"; routedata.values["controller"] = "foo"; // additional route parameter routedata.values["foo"] = "bar"; icontroller foocontroller = new foocontroller(); var rc = new requestcontext(new httpcontextwrapper(httpcontext), routedata); foocontroller.execute(rc); 

personally use approach handling errors inside application. put in application_error , execute error controller custom error pages staying in context of initial http request. place complex objects inside routedata hash , complex objects action parameters. use pass actual exception occurred error controller action.


update:

in order parse url route data tokens taking account current routes could:

var request = new httprequest(null, "http://foo.com/home/index", "id=1"); var response = new httpresponse(new stringwriter()); var httpcontext = new httpcontext(request, response); var routedata = routetable.routes.getroutedata(new httpcontextwrapper(httpcontext)); var values = routedata.values; var action = values["action"]; var controller = values["controller"]; 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -