如何火OnActionExecuting在网页API控制器? [英] How to fire OnActionExecuting in Web Api controller?

查看:108
本文介绍了如何火OnActionExecuting在网页API控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的API端点使用asp.net MVC(4)网页API控制器。

My api endpoints are using asp.net mvc (4) web api controllers.

是否有任何事件类同MVC怎么还OnActionExecuting?

Are there any events similiar to how mvc has OnActionExecuting?

另外,如何我访问Request对象中查找,如果要求有授权令牌?

Also, how to I access the Request object to lookup if the request has the authorization token?

推荐答案

使用<一个href=\"http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs\">action过滤器。

public class MyActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
         //use filterContext.HttpContext.Request...
    }
}

有关您的控制器动作,应用属性

For your controller action, apply the attribute

[MyActionFilter]
public Action MyAction(...)
{
    //...
}


由于Satpal在他的评论中提到的,你可能真的想使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute%28v=vs.108%29.aspx\">AuthorizeAttribute授权您操作的访问。

这篇关于如何火OnActionExecuting在网页API控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆