我正在尝试使用 HttpActionContext 将数据(请求的标头)和ActionFilter中的数据传递到我的控制器中,但是我不知道该怎么做。我正在尝试将变量loggedInUserID
传递到控制器中。
在本文中:ASP.NET MVC Pass object from Custom Action Filter to Action是使用ActionExecutingContext而不是使用HttpActionContext传递的对象。
代码:
BaseApiController:
[TestActionFilter]
public class BaseApiController : ApiController
{
}
过滤器:
public class TestActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
//Checks if header is method Get and has attribute
if ((actionContext.Request.Method.Method == "GET") && (actionContext.Request.Headers.GetValues("loggedInUser").First() != null))
{
string LoggedInUserID = actionContext.Request.Headers.GetValues("loggedInUser").First();
}
base.OnActionExecuting(actionContext);
}
}
控制器:
public class RedirectController : BaseApiController
{
//Retrieve entire DB
ApiBrokerLocalDB dbProducts = new ApiBrokerLocalDB();
//Get all data by customerID
[System.Web.Http.AcceptVerbs("GET")]
[System.Web.Http.HttpGet]
[System.Web.Http.Route("RedirectApi")]
public Customer RedirectApi()
{
Customer t = dbProducts.Customers
.Where(h => h.customerID == 1)
.FirstOrDefault();
return t;
}
}
希望有人可以提供帮助。
谢谢!