asp.net mvc - MVC - Redirect inside the Constructor -
i know how able redirect request inside controller constructor if need it?.
for example: inside constructor need initialize object dynamic value, in cases don't want , in case want redirect other place. @ same way rest of constructor not executed neither "original following action".
how can it? thank you
edit #1
initially used:
public override void onactionexecuting(actionexecutingcontext filtercontext)
there redirect other controller/action/url, later in time, needed change controller, initialize variable in constructor , have code needs redirect request :p
i need because onactionexecuting executes after controller constructor. , in logic, redirect needs done there.
performing redirects inside controller constructor not practice because context might not initialized. standard practice write custom action attribute , override onactionexecuting method , perform redirect inside. example:
public class redirectingactionattribute : actionfilterattribute { public override void onactionexecuting(actionexecutingcontext filtercontext) { base.onactionexecuting(filtercontext); if (someconditionismet) { filtercontext.result = new redirecttorouteresult(new routevaluedictionary(new { controller = "someother", action = "someaction" })); } } }
and decorate controller redirect attribute. extremely careful not decorate controller redirecting attribute or going run endless loop.
so could:
[redirectingaction] public class homecontroller : controller { public actionresult index() { // action never going execute if // redirecting condition met return view(); } } public class someothercontroller : controller { public actionresult someaction() { return view(); } }
Comments
Post a Comment