【问题标题】:Redirect to view from global.asax not working从 global.asax 重定向到视图不起作用
【发布时间】:2019-02-24 00:59:33
【问题描述】:

如何从 global.asax 重定向到视图?

下面是我的代码。它给了我 HTTP 错误 500.19 - 内部服务器错误。

public void Application_BeginRequest(object sender, EventArgs e)
    {
        if (ConfigurationManager.AppSettings["MaintenanceMode"] == "true")
        {
            //  if (!Request.IsLocal)
            //  {
            Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
            Server.ClearError();
            Response.Clear();

            HttpContext.Current.Response.Redirect("~/Views/Account/MaintenancePage.cshtml");
            // }
        }
    }

【问题讨论】:

  • 试试HttpContext.Current.Response.Redirect("/Account/MaintenancePage");
  • 我试过了。但它显示“页面未正确重定向”
  • 尝试将 false 传递给第二个参数,例如 HttpContext.Current.Response.Redirect("/Account/MaintenancePage", false);。另外,确保/Account/MaintenancePage 是有效的网址
  • 试过了。相同的消息““页面没有正确重定向”:-(
  • 并且您已经确认可以在浏览器中浏览到/Account/MaintenancePage

标签: c# asp.net asp.net-mvc iis


【解决方案1】:

Account 控制器中添加MaintenancePage GET 操作,如下所示:

[AllowAnonymous]
[HttpGet]
public IActionResult MaintenancePage()
{
    return View();
}

然后再指向这个action这样防止无限重定向循环:

HttpContext.Current.RewritePath("/Account/MaintenancePage");

希望对您有所帮助。

【讨论】:

  • 它给了我 StackOverflow 异常
  • 如果您已经来自“/Account/MaintenancePage”,请签入Application_BeginRequest 方法
  • 如何检查?
  • 我查过了。不,我在帐户/索引页中
【解决方案2】:

在维护模式或错误检查的情况下,我使用 Server.Transfer 重定向到静态 html 页面。

Server.Transfer("/Error.html");

我将 Error.html 保留在项目的根目录中。

【讨论】:

  • 它是一个 MVC 应用程序。所以我没有保留在根目录中,而是在 views/account 文件夹中创建了一个视图
  • 顺便说一下,静态页面只是一个文件,因此出于维护目的考虑这种情况而不是控制器操作是有意义的。 stackoverflow.com/questions/17949460/…
  • 已有一个视图文件夹。我不想复制视图。
猜你喜欢
  • 2013-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-03
  • 2015-08-12
  • 2011-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多