【问题标题】:asp.net mvc validate [HttpPost] ActionResult()asp.net mvc 验证 [HttpPost] ActionResult()
【发布时间】:2014-10-28 19:15:02
【问题描述】:

我需要在控制器中实现一个 ActionFilterAttribute [POST] ActionResult()。问题是,如果验证失败,我会尝试“重定向”到某个页面……但它不起作用。验证运行,但随后返回到ActionResult() 下一行,最后当视图返回时,才“重定向”到验证中列出的页面。最终我需要的是停止 ActionResult() 语句并“重定向”到验证中列出的页面。我试过OnActionExecuting()OnActionExecuted() 但没有任何效果

我需要...

filterContext.HttpContext.Response.Redirect (loginUrl, true);

逃跑,“重定向”指示的页面

我的代码:

[HelperSomeValidations("")]
[HttpPost]
public ActionResult Create(Pais pais)
{
  try
  {
    PaisBLL.saveNew(pais);
  }
  catch (Exception ex) 
  {
    ViewBag.error = ex;
    return View(“Error”);
  }
  return RedirectToAction(“Index”);
}

public class HelperSomeValidations : ActionFilterAttribute
{

  public HelperSomeValidations(String permiso)
  {
    this.permiso = permiso;
  }

  public override void OnActionExecuting(ActionExecutingContext filterContext)
  {
    var user = filterContext.HttpContext.Session["coco"];
    if (user == null) //validates if the user just login
    {
      //send them off to the login page
      var url = new UrlHelper(filterContext.RequestContext);
      var loginUrl = url.Content(“~/Usuario/Login”);
      filterContext.HttpContext.Response.Redirect(loginUrl, true);
    }
    else
    {
      if (permission != “”)
      {
        //does some validations with “permission”
      }
    }
 }

}

谢谢!

【问题讨论】:

  • 使用动作过滤器吗?我可以使用AuthorizeAttribute 发布一个示例,这似乎更适合这里。
  • @webnoob: AuthorizeAttributeActionFilterAttribute 都是 FilterAttributes。相同的原则适用于两者。
  • @StriplingWarrior 哎呀,又出现了那个术语……谢谢。我会留下评论以防其他人感到困惑:)

标签: c# asp.net-mvc


【解决方案1】:

我知道这并不能解决您发布的问题,但我认为这是一个更好的解决方案。我个人会在这里使用AuthoriseAttribute,因为这是它的设计目的。

public class Authorise : AuthorizeAttribute
{
    private readonly string _permissionSystemName;

    public Authorise()
    {
    }

    public Authorise(string permissionSystemName)
    {
        _permissionSystemName = permissionSystemName;
    }

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        //DO some logic and return True or False based on whether they are allowed or not.

        return false;
    }

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        filterContext.Result = new RedirectToRouteResult(
            new RouteValueDictionary(
                new
                {
                    area = filterContext.HttpContext.Request.RequestContext.RouteData.Values["area"],
                    controller = "Generic",
                    action = "PermissionDenied"
                })
            );
    }
}

用法如下:

[Authorise("SomePermissionName")]
public class MyController : Controller
{

}

【讨论】:

    【解决方案2】:

    您需要将filterContext.Result 设置为RedirectResult,而不是调用filterContext.HttpContext.Response.Redirect(loginUrl, true)

    【讨论】:

      猜你喜欢
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多