c# - Execute multiple controller actions in one call with MVC.Net 5 -
c# - Execute multiple controller actions in one call with MVC.Net 5 -
we have upgraded our code base of operations .net 4.0 .net 4.5.1 , mvc 2.0 mvc 5.2.2.
we have custom method in our base of operations controller class allowed update multiple parts of our views within single request. since upgrading, no longer works.
original code:
class="lang-cs prettyprint-override">protected void includeaction(string actionname, string controllername, object routevalues) { //get url routevaluedictionary routes = null; if (routevalues != null) routes = new routevaluedictionary(routevalues); else routes = new routevaluedictionary(); routes.add("action", actionname); if (!string.isnullorempty(controllername)) routes.add("controller", controllername); else routes.add("controller", this.controllercontext.routedata.values["controller"].tostring()); var url = routetable.routes.getvirtualpath(this.controllercontext.requestcontext, routes).virtualpath; //rewrite path system.web.httpcontext.current.rewritepath(url, false); ihttphandler httphandler = new mvchttphandler(); httphandler.processrequest(system.web.httpcontext.current); }
we receive errors on httphandler.processrequest
call. used technique in number of places. after much googling seemed should utilize server.transferrequest
instead.
new code
class="lang-cs prettyprint-override">protected void includeaction(string actionname, string controllername, object routevalues) { //get url routevaluedictionary routes = null; if (routevalues != null) routes = new routevaluedictionary(routevalues); else routes = new routevaluedictionary(); routes.add("action", actionname); if (!string.isnullorempty(controllername)) routes.add("controller", controllername); else routes.add("controller", this.controllercontext.routedata.values["controller"].tostring()); var url = routetable.routes.getvirtualpath(this.controllercontext.requestcontext, routes).virtualpath; //rewrite path system.web.httpcontext.current.rewritepath(url, false); system.web.httpcontext.current.server.transferrequest(url, true); }
when called code this:
class="lang-cs prettyprint-override">includeaction("optinbanner", "person"); includeaction("navmenu", "person"); homecoming transfer(returnurl);
our new code generates error:
class="lang-none prettyprint-override">type: system.invalidoperationexception message: transferrequest cannot invoked more once. stack trace: @ system.web.httpserverutility.transferrequest(string path, boolean preserveform) @ myproject.mynamspace.mybasecontroller.includeaction(string actionname, string controllername, object routevalues) @ myproject.mynamspace.mybasecontroller.includeaction(string actionname, string controllername) @ myproject.mynamspace.mycontroller.myaction(boolean mychoice, string returnurl) @ .lambda_method(closure , controllerbase , object[] )
since message plainly says cannot phone call transferrequest more once, code needs execute 2 controller actions in add-on redirecting , performing 3rd action, thought i'd revert old code. however, generates error:
class="lang-none prettyprint-override">type: system.invalidoperationexception message: 'httpcontext.setsessionstatebehavior' can invoked before 'httpapplication.acquirerequeststate' event raised. stack trace: @ system.web.routing.urlroutinghandler.processrequest(httpcontextbase httpcontext) @ myproject.mynamspace.mybasecontroller.includeaction(string actionname, string controllername, object routevalues) @ myproject.mynamspace.mybasecontroller.includeaction(string actionname, string controllername) @ myproject.mynamspace.mycontroller.myaction(boolean mychoice, string returnurl) @ .lambda_method(closure , controllerbase , object[] )
for function, how can retain original behavior, without errors, had under .net 4.0 , mvc 2.0 while using newer framework , mvc?
after lot of research, came this:
class="lang-cs prettyprint-override">protected void includeaction(string actionname, string controllername, object routevalues) { string targetcontroller = null; if (!string.isnullorwhitespace(controllername)) { targetcontroller = controllername; } else { targetcontroller = this.controllercontext.routedata.values["controller"].tostring(); } htmlhelper htmlhelper = new htmlhelper( new viewcontext( this.controllercontext, new webformview(this.controllercontext, actionname), this.viewdata, this.tempdata, this.response.output ), new viewpage() ); htmlhelper.renderaction(actionname, targetcontroller, routevalues); }
the htmlhelper constructed new viewcontext. new viewcontext constructed much of info current controllercontext , current response object's textwriter (this.response.output
).
calling renderaction
on htmlhelper render chosen controller-action results response stream, allowing phone call multiple controller actions other controller actions , letting our client side ajax framework apply results right portions of page.
for places okay throw out pending response stream, can still utilize server.transferrequest
. allows append multiple controller-action results same response stream, server.
c# invalidoperationexception asp.net-mvc-5.2
Comments
Post a Comment