如果不符合某些條件(例如HttpContext.Current.Request.Form [「key」]。toString()),我希望鎖定對html頁面的訪問(例如./manual/manual_A/index.html) 。 .Equals(「a」)),我希望重定向到特定的視圖(例如./errorPage/),否則繼續(例如/ index)。 In register Route I add:如何限制對ASP.NET MVC中某些頁面的訪問?
routes.MapRoute(
"ErrorPage",
"errorPage/",
new { controller = "Home", action = "ErrorPage" }
);
routes.MapRoute(
"Path",
"{*_request}",
new { controller = "Home", action = "Index" }
);
用於讀取所有請求。 在控制器首頁
[CustomAuthorize]
public ActionResult Index()
{
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
if(!condition)
filterContext.Result = RedirectToAction("PageError");
}
OnActionExecuting執行每個請求,然而,RedirectToAction不會發生。 任何提示我做錯了什麼?
你redirectToAction和你的路由的行動不匹配,你可能也想檢查一下(PageError!= ErrorPage) – Tommy 2010-10-07 13:50:32
你是否絕對肯定你的條件正在評估爲false?你可以在調試器下驗證它嗎?另請記住,授權在其他操作過濾器之前發生。如果您的[CustomAuthorize]掛鉤了OnAuthorization,您可能實際上希望從該過濾器中設置Result,而不是使用單獨的操作過濾器執行此操作。 – Levi 2010-10-07 16:54:52
嗨,我找到了解決我的問題。在白天把代碼提供。感謝大家的建議。 – user468451 2010-10-08 04:36:08