我有一个ASP.NET MVC网站,它也有WEB API和OData端点。有没有办法让网站脱机,但保持API和OData工作?
我尝试在根目录上放置App_Offline.htm,但这使所有区域都脱机。
答案 0 :(得分:5)
写一个ActionFilterAttribute
并将其注册为全局过滤器:
public class MyFilterAttribute : ActionFilterAttribute
{
private string _offlineUrl;
public MyFilterAttribute(string offlineUrl)
{
_offlineUrl = offlineUrl;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// considering you have a API controller
// and you have a Boolean app setting named BringMVCRoutesOffline
// to activate this filter form web.config
// in real world scenario you could check area or what URL
// or route data you want
if (Convert.ToBoolean(ConfigurationManager.AppSettings["BringMVCRoutesOffline"])
&& filterContext.RouteData.Values.ContainsKey("controller")
&& !filterContext.RouteData.Values["controller"]
.ToString().ToLower() == "api"
// avoiding infinite loop
&& filterContext.HttpContext.Request
.AppRelativeCurrentExecutionFilePath != _offlineUrl)
{
filterContext.Result = new RedirectResult(_offlineUrl);
}
}
}
并在App_Start> FilterConfig add:
filters.Add(new MyFilterAttribute("~/error/offline"));
答案 1 :(得分:0)
将WEB API和OData端点部署为单独的IIS applications,以便App_Offline.htm
可用于关闭一个应用程序而不影响其他部署。您可以使用嵌套IIS应用程序的现有URL完成此操作。