简体   繁体   English

如何访问 ActionExecutingContext 中的 Request.Properties

[英]How to access Request.Properties in ActionExecutingContext

How to access Request.Properties in ActionExecutingContext ?如何访问ActionExecutingContext Request.Properties

public class UserFilter : ActionFilterAttribute
{
    public void OnActionExecuting(ActionExecutingContext actionContext)
    {
        // Properties is not part of the Request here, so I can't access it
        // Here Request is of type System.Web.HttpRequestBase
        actionContext.HttpContext.Request.Properties.Add("UserData", new UserData());
    }
}

I can do it in ApiController :我可以在ApiControllerApiController

public class HomeController : ApiController
{
    public HomeController()
    {
        // Here I can do it (here Request is of type
        // System.Net.Http.HttpRequestMessage
        this.Request.Properties.Add("UserData", new UserData());
    }
}

For ApiController you need use another ActionFilterAttribute (located in System.Web.Http.Controllers namespace):对于ApiController您需要使用另一个ActionFilterAttribute (位于System.Web.Http.Controllers命名空间中):

    using System.Web.Http.Controllers;
    using System.Web.Http.Filters;

    public class UserFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
                    // Properties is part of the Request here, you can access it
                    // actionContext.Request.Properties
        }
    }

actionContext.ActionDescriptor.Properties.Add("UserData", new UserData());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET VNext Request.Properties - ASP.NET VNext Request.Properties 如何在ActionExecutingContext中访问ActionDescriptor的MethodInfo.ReturnType? - How can I access the MethodInfo.ReturnType for an ActionDescriptor within the ActionExecutingContext? 如何使用Moq模拟ActionExecutingContext - How to mock ActionExecutingContext with moq 如何设置ActionExecutingContext状态代码 - How to set ActionExecutingContext status code 如何在 IActionFilter Asp.net core 中读取 OnActionExecuting(ActionExecutingContext context) 中的请求正文 - How to read request body in OnActionExecuting(ActionExecutingContext context) in IActionFilter Asp.net core 如何在 c# 中从 System.Mvc.Web.ActionExecutingContext 获取请求正文数据 - How to get request body data from System.Mvc.Web.ActionExecutingContext in c# 我可以在ActionExecutingContext对象中找到请求类型吗 - Can i find request type in ActionExecutingContext object 如何从 ActionExecutingContext 获取路由模板 - how to get a template of route from ActionExecutingContext 无法访问OnActionExecuting下的User.Identity(ActionExecutingContext filterContext) - Cannot access to User.Identity under OnActionExecuting(ActionExecutingContext filterContext) 如何知道 model 或 ActionExecutingContext 参数是否是实体(asp.net core 5) - How to know if a model or ActionExecutingContext argument is an entity (asp.net core 5)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM