Wie*_*hie 4 c# asp.net-mvc asp.net-web-api asp.net-core
我有一个 ASP.NET 控制器,其中每个方法都有一个共享参数。通过属性路由,我可以在控制器的路由中添加这个参数。
但是,我仍然需要在每个方法中添加该参数以及验证属性。有没有办法让我在一个地方进行验证或避免将其传递给每个方法?
这是当前的工作代码:
[ApiController]
[Route("[controller]/{name}")]
public class ExampleController : ControllerBase
{
    [HttpGet]
    public string Sample([StringLength(10)][FromRoute]string name)
    {
    }
    [HttpGet]
    [Route("defaults")]
    public string GetDefaults([StringLength(10)][FromRoute]string name)
    {
    }
    [HttpGet]
    [Route("objects/{id}")]
    public string Sample([StringLength(10)][FromRoute]string name, [FromRoute]string id)
    {
    }
}
有可能得到接近于此的东西吗?(我知道控制器上的验证参数无效,但我只想应用一次)
[ApiController]
[StringLength(10)]
[Route("[controller]/{name}")]
public class ExampleController : ControllerBase
{
    [HttpGet]
    public string Sample()
    {
    }
    [HttpGet]
    [Route("defaults")]
    public string GetDefaults()
    {
    }
    [HttpGet]
    [Route("objects/{id}")]
    public string Sample([FromRoute]string id)
    {
    }
}
您可以使用自定义操作过滤器来验证 name 参数:
public class ValidateNameParameterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.ActionParameters.ContainsKey(name))
        {
            // the trick is to get the parameter from filter context
            string name = filterContext.ActionParameters[name] as string;
            // validate name
            if (/*name is not valid*/)
            {
                // you may want to redirect user to error page when input parameter is not valid
                filterContext.Result = new RedirectResult(/*urlToRedirectForError*/);
            }
            base.OnActionExecuted(filterContext);
        }
    }
}
现在您可以将过滤器应用于您的控制器或特定操作:
[ApiController]
[Route("[controller]/{name}")]
[ValidateNameParameter] // <-- execute this for all actions in the controller
public class ExampleController : ControllerBase
{
    [HttpGet]
    public string Sample([StringLength(10)][FromRoute]string name)
    {
    }
    [HttpGet]
    [Route("defaults")]
    public string GetDefaults([StringLength(10)][FromRoute]string name)
    {
    }
    [HttpGet]
    [Route("objects/{id}")]
    // [ValidateNameParameter] // <-- execute for this specific action
    public string Sample([StringLength(10)][FromRoute]string name, [FromRoute]string id)
    {
    }
}
有关更多信息,请参阅本教程。
正如Hooman Bahreini所说,您可以自定义一个继承ActionFilterAttribute的动作过滤器,并将其用作控制器上的属性。在Asp.net core中,ActionArguments替换ActionParameters 
public class ValidateNameParameterAttribute: ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.ActionArguments.ContainsKey("name"))
        {
            string name = filterContext.ActionArguments["name"] as string;
            if(name!=null && name.Length>10)
            {
                filterContext.Result = new BadRequestObjectResult("The length of name must not exceed 10");
            }
        }
    }
}
有关 ASP.NET Core 中过滤器的更多详细信息,您可以参考这里。
| 归档时间: | 
 | 
| 查看次数: | 5584 次 | 
| 最近记录: |