How to config Asp.net Mvc for redirecting every request to configuration page?

偶尔善良 提交于 2019-12-24 09:13:15

问题


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:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/999480/how-to-config-asp-net-mvc-for-redirecting-every-request-to-configuration-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!