我希望锁定对html页面的访问权限(例如/manual/manual_A/index.html) 如果不满足某个条件(例如HttpContext.Current.Request.Form [“key”]。toString()。Equals(“a”)),我希望重定向到特定视图(例如/ errorPage /),否则继续(例如/ index)。 在注册路线I中添加:
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不会发生。 关于我做错了什么的暗示?
答案 0 :(得分:0)
您需要设置如下所示的filterContext.Result
,然后重定向到您的错误页面。
filterContext.Result = new EmptyResult();
RedirectToAction("PageError");
这应该解决问题。
答案 1 :(得分:0)
这是我的解决方案。 在Globax.asax中:
public static void RegisterRoutes(RouteCollection routes)
{
routes.RouteExistingFiles = true;
routes.MapRoute("PageError", "PageError/", new { controller = "Home", action = "PageError" });
routes.MapRoute("PathPageStatic", "PathPageStatic/", new { controller = "Home", action = "PathPageStatic" });
routes.MapRoute("Path", "{*_request}", new { controller = "Home", action = "Index"});
}
在Controller中,我使用Session来记忆条件:
public ActionResult PathPageStatic()
{
if (condition)
{
Session["key"] = condition;
return View("Index");
}
else
return RedirectToRoute("PageError");
}
[UserAuthorize]
public ActionResult Index()
{
string patternStaticFile = @"\.*.(htm|html)";
if (Regex.IsMatch(HttpContext.Request.Url.AbsoluteUri, patternStaticFile))
{
string pathFile = HttpContext.Request.Url.LocalPath;
if (System.IO.File.Exists(pathFile))
return new FilePathResult(pathFile, MimeType(pathFile));
else return null;
}
else
return View();
}
public ActionResult PageError()
{
return View("PageError");
}
在AuthorizeAttribute中:
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (HttpContext.Current.Session["key"] == null)
filterContext.HttpContext.Response.Redirect("~/PageError", true);
}