【问题标题】:How to pass ControllerContext to Attribute如何将 ControllerContext 传递给 Attribute
【发布时间】:2014-02-26 15:16:20
【问题描述】:

我想使用来自 Attribute\Filter 构造函数内部的 ControllerContext 或 ActionContext 的信息。我该怎么做?尝试将 ControllerContext 作为参数传递给动作中的属性,但没有成功。

【问题讨论】:

  • 过滤器实例是在每个请求的基础上创建的not,因此您不能将 ControllerContext(它是每个请求的特定上下文)传递给过滤器构造函数。

标签: asp.net-mvc asp.net-web-api asp.net-web-api2


【解决方案1】:
public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
           var controllerContext =  filterContext.Controller.ControllerContext;

关于动作执行。你不能了解施工背景

【讨论】:

    【解决方案2】:
       [HttpGet]
        public ActionResult VideoStream(int id = 0)
        {
         }
        public override void ExecuteResult(ControllerContext context)
        {
            string routedata = context.RequestContext.RouteData.Values["id"].ToString();
            //The File Path 
            var videoFilePath = HostingEnvironment.MapPath("~/CombineFile/Tanvir.mp4");
            //The header information 
    
        } 
    

    【讨论】:

      猜你喜欢
      • 2022-10-17
      • 1970-01-01
      • 2020-05-07
      • 2012-10-22
      • 2020-06-30
      • 2014-07-15
      • 2021-11-05
      • 2015-08-31
      • 2012-07-03
      相关资源
      最近更新 更多