【问题标题】:Not able to add custom Header text in WebApi GET无法在 WebApi GET 中添加自定义标题文本
【发布时间】:2020-05-29 04:06:20
【问题描述】:

我有一个完全可用的 Visual Studio WebApi 解决方案,我现在正在尝试在返回响应标头中添加一些自定义标头信息,但在客户端我只能接收标准的标头信息,如下所示:

这里是 GetCustomer WebApi 的代码:

    public class CustomerController : ApiController
    {

        [HttpGet]
        public JsonResult<List<Models.Customer>> GetCustomers()
        {

            EntityMapper<DataAccessLayer.Customer, Models.Customer> mapObj = new EntityMapper<DataAccessLayer.Customer, Models.Customer>();
            List<DataAccessLayer.Customer> custList = DAL.GetAllCustomers();
            List<Models.Customer> customers = new List<Models.Customer>();

            foreach (var item in custList)
            {
                customers.Add(mapObj.Translate(item));
            }

            // Setting Header  
            HttpContext.Current.Response.Headers.Add("Some-Header", "Some-Text"));


            // Returning List of Customers Collections  
            return Json<List<Models.Customer>>(customers);
        }

因此 HttpContext.Current.Response.Headers.Add("Some-Header", "Some-Text")); 不会产生错误,但会被完全忽略。

知道如何解决这个问题吗?

【问题讨论】:

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


    【解决方案1】:

    好的。我发现自己的问题。 由于 CORS,我必须明确指示响应以公开自定义标头。

    添加:

    HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers", "Some-Header");

    解决了问题!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多