我刚刚被抛到ASP.NET MVC项目上,任务是创建一个“离线”页面,只要数据库中的标志设置为true就会显示出来。我检查了这个值,如果它是真的,我希望整个网站对于任何请求都是离线的。我做了一个ActionFilter到最后并将其添加到所有请求中。然后,当请求页面时,我会相应地进行过滤。但是,这也会过滤离线页面本身,因此我做了一行检查以查看它是否为离线页面 - 如果是,它只是返回。问题是登录页面正在如此转发:/ Account/Login?ReturnUrl =%2FOffline,最终显示登录页面。我该如何解决这个问题?ASP.NET MVC“离线”页面
public class DownForMaintenanceActionFilter : ActionFilterAttribute, IActionFilter
{
public DownForMaintenanceActionFilter()
{
}
void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
{
// If they are actually visiting the Offline page already, abort.
if (HttpContext.Current.Request.RawUrl.Contains("Offline"))
{
this.OnActionExecuting(filterContext);
return;
}
// We want to go to the "Down For Maintenance" Page if any of the following is true:
bool siteIsUp = true;
// Some other code here that sets siteIsUp = false
if (!siteIsUp)
RedirectToMaintenancePage(filterContext);
else
this.OnActionExecuting(filterContext);
}
private void RedirectToMaintenancePage(ActionExecutingContext filterContext)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "controller", "Offline" },
{ "action", "Index" }
});
}
}
我该怎么做我想做的事?这是最好的方法吗? 另请参阅:如何防止用户访问此页面时,网站不是停机?谢谢!
是不是一个更好的approuch,使一个批处理文件,添加或删除一个app_offline.htm到网站 – lordkain
我不得不使它自动为客户谁不知道这样的事情。 – skaz