没有在响应中获得标头

时间:2017-08-02 06:53:07

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

我在WEB API中设置此标头但在客户端

上获得响应
HttpContext.Current.Response.Headers.Add("x-total-records", documentData.TotalItemsCount.ToString(CultureInfo.InvariantCulture));

为什么它没有响应

1 个答案:

答案 0 :(得分:0)

在你的mvc函数中执行此操作

Request.Properties.Add("x-total-records", documentData.TotalItemsCount.ToString(CultureInfo.InvariantCulture));

并创建一个像这样的过滤器

public class CustomHeaderFilter : ActionFilterAttribute
{
    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        if (actionExecutedContext.Request.Properties.Any(p => p.Key == "x-total-records"))
        {
            actionExecutedContext.Response.Content.Headers.Add("Access-Control-Expose-Headers", "x-total-records");
            actionExecutedContext.Response.Content.Headers.Add("x-total-records", actionExecutedContext.Request.Properties["x-total-records"].ToString());
        }


    }
}

Hopfuly这将解决您的问题