如何配置 Asp.net Mvc 将每个请求重定向到配置页面?

发布于 2024-07-24 20:26:36 字数 571 浏览 3 评论 0原文

对于Asp.net Mvc项目,当用户(应该是该网站的管理员)第一次访问该网站时,我需要将每个请求重定向到配置页面。 此操作类似于默认登录页面(如果访问被拒绝,每个请求都将重定向到默认登录页面)。

用户配置好配置文件后,路由表将被映射到普通控制器。

诗。 此页面应帮助管理员检测错误配置并易于部署。

更新#1 我尝试在 Codeplex 上使用 ASP.NET MVC WebFormRouting Demo 。 但是当用户访问某些现有页面(例如“~/AccessDenied.aspx”或“~/web.config”)时,我无法重定向。

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

谢谢

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

佼人 2024-07-31 20:26:36

根据您的描述,这似乎是一个授权问题,因此我建议使用自定义 Authorize 属性类(继承自 AuthorizeAttribute)。

从这里您可以重写 OnAuthorization 方法,您可以在其中检查用户是否已完成所需的配置步骤并相应地设置 filterContext.Result。 基本实现如下所示(假设您有有效的 /Account/Configure 路由):

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;
        }
    }
}

您可以在 StackOverflow 上找到有关如何创建自定义 AuthorizeAttribute 类的其他示例。

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.

花开雨落又逢春i 2024-07-31 20:26:36

2 个想法:

  • 在路由表顶部使用一个包罗万象的规则,并对其施加约束以检查配置状态
  • 将此检查的代码放入 GlobalAsax 中的 Application_BeginRequest 中,

了解包罗万象的想法的详细信息:

  • 使用 url 创建规则“{*path}”并将其放在列表的第一位
  • 创建一个约束,仅在配置尚未完成的情况下激活此规则
  • 创建一个简单的控制器,例如 ConfigController,其单个操作只执行 RedirectToUrl(" config.aspx")

但是 Application_BeginRequest 中的解决方案会更简单,因为处理此问题的整个代码都集中在一个地方

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

要走干脆点 2024-07-31 20:26:36

现在,我可以应用 我的技术另一个问题来解决这个问题。 通过在应用程序启动时在静态实例中保留一些值。 请看下面的代码。

部分ConfigBootstapper.cs

public class ConfigBootstapper
{
    public static EnableRedirectToConfigManager = false;
}

部分ConfigModule.cs

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

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

部分Global.asax

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

PS。 不要忘记在重定向之前检查一些导致无限循环的条件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文