views:

391

answers:

3

For Asp.net Mvc project, I need to redirect every request to configuration page when user(should be admin of this website) visit this website at the first time. This operation like default login page(every request will be redirect to default login page if access denied).

After user config the configuration file, Route table will be mapped to normal controllers.

Ps. This page should helps Admin for detect error configuration and easy to deploy.

Update #1 I try to use ASP.NET MVC WebFormRouting Demo on Codeplex. But I can't redirect when user visit some existing page like "~/AccessDenied.aspx" or "~/web.config".

routes.MapWebFormRoute("RedirectToConfig", "{*anything}", "~/App_Config");

Thanks,

+1  A: 

2 ideas:

  • Use a catch-all rule on top of your routing table and put a constraint on it that checks for the config status
  • Put the code for this check in Application_BeginRequest in GlobalAsax

Details for the catch-all idea:

  • Create a rule with url "{*path}" and put it first in your list
  • Create a constraint to activate this rule only in case the configuration is not done yet
  • Create a simple controller e.g. ConfigController with a single action that does nothing but a RedirectToUrl("config.aspx")

But the solution in Application_BeginRequest would be simpler, since the whole code to handle this in one place

chris166
So, I need to create custom RouteBase class for catch all rule by using RouteTable class.
Soul_Master
I think a custom constraint is enough
chris166
But I don't have any controller for handler this request. I use only single aspx file. How to route any request to aspx file like '~/App_Config/Default.aspx' or '~/App_Config'?
Soul_Master
So you're mixing WebForms with MVC? Doesn't seem like a good idea. I suggest creating a simple controller for the App_Config/Default.aspx file.
erikkallen
Because I want to create seperated system for configuration only.
Soul_Master
+2  A: 

From your description, this appears to be an authorization concern, so I would recommend a custom Authorize attribute class (inherit from AuthorizeAttribute).

From here you can override the OnAuthorization method where you can check if the user has completed your required configuration steps and set the filterContext.Result accordingly. A basic implementation would look something like this (this assumes you have a valid /Account/Configure route):

public class CustomAuthorizeAttribute : AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        base.OnAuthorization(filterContext);

        var user = ; // get your user object

        if(user.IsConfigured == false)  // example
        {
            filterContext.Result = new RedirectToRouteResult(
                new RouteValueDictionary
                    {
                        {
                            "ConfigureUserRoute",
                            filterContext.RouteData.Values["ConfigureUserRoute"]
                        },
                        {"controller", "Account"},
                        {"action", "Configure"}
                    });
           return;
        }
    }
}

You can find other examples of how to create a custom AuthorizeAttribute class here on StackOverflow.

James H
No. I need to redirect every request except "~/App_Config/{filename}" to "~/App_Config" when user(should be admin of this website) visit this website at the first time. So, It doesn't authorization concern.
Soul_Master
A: 

Now, I can apply technique from my another question to solve this problem. By keep some value in static instance when application is starting. Please look at the following code.

partial ConfigBootstapper.cs

public class ConfigBootstapper
{
    public static EnableRedirectToConfigManager = false;
}

partial ConfigModule.cs

void HttpApplication_BeginRequest(object sender, EventArgs e)
{
    HttpApplication app = sender as HttpApplication;

    if (ConfigBootstapper.EnableRedirectToConfigManager)
    {
        app.Response.Redirect("~/App_Config");
    }
}

partial Global.asax

protected void Application_Start()
{
    [logic for setting ConfigBootstapper.EnableRedirectToConfigManager value]
}

PS. Don't forget to checking some condition that cause infinite-loop before redirect.

Soul_Master