Create a one time setup screen the first time the application loads based on criteria?
In my application I would like to something like this
if(settings = null) {
// r开发者_如何学Cedirect the request to example.com/setup
}
I have tried to use webactivator for this but it seems it's to early to to a redirect the request. Another way is to have a base controller and check every request but that seems like a bad idea.
So, can anyone recommend a solution?
I wouldn't go with the custom ControllerFactory route. I've had mixed results when using custom controller factories. Also if you will be using ServiceStack or some other framework, you will be forced to use their controller factory.
IMHO the preferred way is to use Base controller and handle the OnActionExecuted
there. I use it to inject Configuration into the ViewBag
for all controllers that are inheriting the Base, so that the Configuration is available via ViewBag
in my Views.
You can also put first run checks in here, like this:
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
// store administration configuration for Views
ViewBag.AdminConfig = AdminConfig;
// check for setup config; we are on the first run, if it doesn't exist
if (Session["IsFirstRun"] == null && SetupConfigProvider.IsFirstRun())
{
// clear the current response to prevent unwanted behaviour
Response.Clear();
// redirect to the Setup controller
filterContext.Result = RedirectToAction("Index", "Setup");
}
}
In the Index
action of the Setup
controller (inside the POST version of the Index action, that is), you'd be setting the Session["IsFirstRun"] = false
and you are good to go.
Since SetupConfigProvider.IsFirstRun()
is often an expensive IO operation, the caching the value in Session prevents the application to always go look into datastore (or disk). This will happen only the first time when both Session["IsFirstRun"]
is null and IsFirstRun()
returs true
.
I needed to do the same thing for my opensource project WeBlog. I ended up using a custom controller factory. If the site is not configured, I redirect to the Setup controller by using the following code:
public class WeBlogControllerFactory : DefaultControllerFactory
{
public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
if (!SiteManager.Any())
{
requestContext.RouteData.Values["action"] = "Index";
requestContext.RouteData.Values["controller"] = "Setup";
return base.CreateController(requestContext, "Setup");
}
return base.CreateController(requestContext, controllerName);
}
}
To register your custom controller factory, just add this line to the application_start method in the global.asax:
ControllerBuilder.Current.SetControllerFactory(new WeBlogControllerFactory());
You are correct, it is too early. DOn't forget that the WebActivator.PreStartUpMethod attribute runs the specified method BEFORE App_Start.
SO in your case, it is too early becasue there is no RequestContext yet I believe.
精彩评论