How to show a page only once in asp.net mvc -
in app, checking if config file available or not, if it's not want redirect install page.
to me best place accomplish application_start
. because it's happening 1 time. if checking in application_start
, write response.redirect
response not available in context
.
i tried other answers in stack overflow redirect in application_start
httpcontext.current.response.redirect
; none worked me.
i don't want in base controller
or filter
because checking logic happen every single request.
my goal check once , it's best when app start.
update 1
i added response.redirect application_start got error this:
application start:
protected void application_start() { arearegistration.registerallareas(); filterconfig.registerglobalfilters(globalfilters.filters); routeconfig.registerroutes(routetable.routes); bundleconfig.registerbundles(bundletable.bundles); response.redirecttoroute( new routevaluedictionary { { "controller", "home" }, { "action", "about" } }); }
but receiving error this:
response not available in context.
description: unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code.
exception details: system.web.httpexception: response not available in context.
if want avoid having filter run every request after setup can this:
redirectattribute.cs (generic example)
public class redirectattribute : actionfilterattribute { private readonly string _controller; private readonly string _action; public redirectattribute(string controller, string action) { _controller = controller; _action = action; } public override void onactionexecuting(actionexecutingcontext filtercontext) { if (filtercontext.actiondescriptor.actionname != _action || filtercontext.actiondescriptor.controllerdescriptor.controllername != _controller) { filtercontext.result = new redirecttorouteresult( new routevaluedictionary(new {controller = _controller, action = _action}) ); } } }
in global.asax.cs above "filterconfig.registerglobalfilters(globalfilters.filters);"
if (/*insert logic check if config file not exist*/) { //replace "setup" , "index" setup controller , action below globalfilters.filters.add(new redirectattribute("setup", "index")); }
now, after user has completed setup, can unload app domain:
httpruntime.unloadappdomain();
please note: you need make sure app has permission unload appdomain. if not, can try file.setlastwritetimeutc(...) on configuration file (appdomain.currentdomain.setupinformation.configurationfile.) unload appdomain.
unloading appdomain "restart" web app , call application_start() again. the filter not added requests since if statement determine app has been configured.
Comments
Post a Comment