Asp.net mvc 如何使筛选器重定向到另一个操作?
Asp.net mvc 如何使筛选器重定向到另一个操作?,asp.net-mvc,redirecttoaction,Asp.net Mvc,Redirecttoaction,重定向到操作受保护,我们只能在操作内部使用它。但是如果我想在过滤器中重定向 public class IsGuestAttribute: ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { if (!Ctx.User.IsGuest) filterContext.Result = (fi
重定向到操作
受保护,我们只能在操作内部使用它。但是如果我想在过滤器中重定向
public class IsGuestAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!Ctx.User.IsGuest)
filterContext.Result = (filterContext.Controller as Controller)
.RedirectToAction("Index", "Home");
}
}
RedirectToAction
只是构造RedirectToRouteResult()
的一个辅助方法,因此您只需创建一个新的RedirectToRouteResult()
并传递一个带有操作值的RouteValueDictionary()
根据@Domenic在下面评论中的代码完成示例:
public class IsGuestAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!Ctx.User.IsGuest)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "controller", "Home" },
{ "action", "Index" }
});
}
}
}
下面是一个代码示例:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!Ctx.User.IsGuest)
{
RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("action", "Index");
redirectTargetDictionary.Add("controller", "Home");
filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
}
}
安全/授权/身份验证筛选器应使用和
我知道我参加聚会有点晚了,但我使用了veggerby的解决方案,并构建了一个可能对某些人有用的助手类,因此我想在这里提供它:
public static class ActionFilterHelpers
{
public static void Redirect(this ActionExecutingContext filterContext, String controller, String action, object routeValues)
{
filterContext.Result = Redirect(controller, action, routeValues);
}
public static ActionResult Redirect(String controller, String action, object routeValues)
{
var routeValues = new RouteValueDictionary();
routeValues["controller"] = controller;
routeValues["action"] = action;
foreach (var keyValue in new ObjectDictionary(routeValues))
routeValues.Add(keyValue.Key, keyValue.Value);
return new RedirectToRouteResult(routeValues);
}
}
我提供了一个返回重定向ActionResult
的静态方法和一个扩展filterContext
的扩展方法。希望有人觉得这个有用
ObjectDictionary
是一个类,它使用反射从构建字典的对象的属性创建字典。我没有包含这些代码,因为我相信在框架中有更好的方法来实现这一点。我还没有找到它,但我不想让其他人继承我潜在的bug。用法:filterContext.RedirectToAction(“登录”、“帐户”)代码>
这是我编写的一个助手类,其中包含一些扩展方法,用于在更多地方提供RedirectToAction功能。这对于OP来说已经太晚了,但希望它能帮助一些人
public static class RedirectHelper
{
// RedirectToAction Extension Methods
public static void RedirectToAction(this HttpResponseBase response, String action, String controller, object routeValues = null, bool endResponse = false)
{
response.RedirectToRoute(CreateRoute(action, controller, routeValues));
if (endResponse) response.End();
}
public static void RedirectToAction(this HttpResponse response, String action, String controller, object routeValues = null, bool endResponse = false)
{
response.RedirectToRoute(CreateRoute(action, controller, routeValues));
if (endResponse) response.End();
}
public static void RedirectToAction(this ActionExecutingContext filterContext, String action, String controller, object routeValues = null, bool endResponse = false)
{
if (endResponse) filterContext.HttpContext.Response.RedirectToAction(action, controller, routeValues, true);
else filterContext.Result = new RedirectToRouteResult(CreateRoute(action, controller, routeValues));
}
public static void RedirectToAction(this ExceptionContext filterContext, String action, String controller, object routeValues = null, bool endResponse = false)
{
if (endResponse) filterContext.HttpContext.Response.RedirectToAction(action, controller, routeValues, true);
else {
filterContext.ExceptionHandled = true;
filterContext.Result = new RedirectToRouteResult(CreateRoute(action, controller, routeValues));
}
}
// Route Value Derivation
public static RouteValueDictionary CreateRoute(String action, String controller, object routeValues = null)
{
RouteValueDictionary result = routeValues != null ?
HtmlHelper.AnonymousObjectToHtmlAttributes(routeValues) :
new RouteValueDictionary();
result["controller"] = controller;
result["action"] = action;
return result;
}
}
还有更多的ControllerContext没有包括在内,但是根据您的需要添加自己的内容应该相当容易。或者只是新建RedirectToRouteResult(新建RouteValueDictionary{{“controller”,“Home”},{“action”,“HomePage”})
如果你可以在下面加一个绿色的重锐器,你也可以把这段代码放在OnException
方法中,只要你设置filterContext.ExceptionHandled=true
您也可以将此代码放入OnException
方法中,只要设置filterContext.ExceptionHandled=true代码>
public static class RedirectHelper
{
// RedirectToAction Extension Methods
public static void RedirectToAction(this HttpResponseBase response, String action, String controller, object routeValues = null, bool endResponse = false)
{
response.RedirectToRoute(CreateRoute(action, controller, routeValues));
if (endResponse) response.End();
}
public static void RedirectToAction(this HttpResponse response, String action, String controller, object routeValues = null, bool endResponse = false)
{
response.RedirectToRoute(CreateRoute(action, controller, routeValues));
if (endResponse) response.End();
}
public static void RedirectToAction(this ActionExecutingContext filterContext, String action, String controller, object routeValues = null, bool endResponse = false)
{
if (endResponse) filterContext.HttpContext.Response.RedirectToAction(action, controller, routeValues, true);
else filterContext.Result = new RedirectToRouteResult(CreateRoute(action, controller, routeValues));
}
public static void RedirectToAction(this ExceptionContext filterContext, String action, String controller, object routeValues = null, bool endResponse = false)
{
if (endResponse) filterContext.HttpContext.Response.RedirectToAction(action, controller, routeValues, true);
else {
filterContext.ExceptionHandled = true;
filterContext.Result = new RedirectToRouteResult(CreateRoute(action, controller, routeValues));
}
}
// Route Value Derivation
public static RouteValueDictionary CreateRoute(String action, String controller, object routeValues = null)
{
RouteValueDictionary result = routeValues != null ?
HtmlHelper.AnonymousObjectToHtmlAttributes(routeValues) :
new RouteValueDictionary();
result["controller"] = controller;
result["action"] = action;
return result;
}
}