C# ApiController获取方法响应头
假设我有一个C# ApiController获取方法响应头,c#,asp.net-mvc,asp.net-web-api,C#,Asp.net Mvc,Asp.net Web Api,假设我有一个ApiController,方法如下: [HttpGet] IEnumerable<MyType> GetSomeOfMyType( ) { return new MyType[ 10 ]; } [HttpGet] IEnumerable GetSomeOfMyType() { 返回新的MyType[10]; } 我想修改其中一个响应头,我将如何转换此方法以允许这样做 我猜我需要手动创建一个响应并将数据序列化到其中,但是如何创建呢 谢谢 [HttpGet] A
ApiController
,方法如下:
[HttpGet]
IEnumerable<MyType> GetSomeOfMyType( )
{
return new MyType[ 10 ];
}
[HttpGet]
IEnumerable GetSomeOfMyType()
{
返回新的MyType[10];
}
我想修改其中一个响应头,我将如何转换此方法以允许这样做
我猜我需要手动创建一个响应并将数据序列化到其中,但是如何创建呢
谢谢
[HttpGet]
ActionResult GetSomeOfMyType( )
{
...
HttpContext.Response.AppendHeader("your_header_name", "your_header_value");
...
return Json(new MyType[ 10 ]);
}
假设您使用JSON进行序列化。否则,您可以使用ContentResult
类和自定义序列化函数,如下所示:
[HttpGet]
ActionResult GetSomeOfMyType( )
{
...
HttpContext.Response.AppendHeader("your_header_name", "your_header_value");
...
return new ContentResult {
Content = YourSerializationFunction(new MyType[ 10 ]),
ContentEncoding = your_encoding, // optional
ContentType = "your_content_type" // optional
};
}
假设您使用JSON进行序列化。否则,您可以使用ContentResult
类和自定义序列化函数,如下所示:
[HttpGet]
ActionResult GetSomeOfMyType( )
{
...
HttpContext.Response.AppendHeader("your_header_name", "your_header_value");
...
return new ContentResult {
Content = YourSerializationFunction(new MyType[ 10 ]),
ContentEncoding = your_encoding, // optional
ContentType = "your_content_type" // optional
};
}
您不应该返回
IEnumerable
,而应该像下面的代码一样返回HttpResponseMessage
,然后您可以修改其标题
:
[HttpGet]
public HttpResponseMessage GetSomeOfMyType( )
{
var response = Request.CreateResponse(HttpStatusCode.OK, new MyType[10]);
//Access to header: response.Headers
return response;
}
您不应该返回
IEnumerable
,而应该像下面的代码一样返回HttpResponseMessage
,然后您可以修改其标题
:
[HttpGet]
public HttpResponseMessage GetSomeOfMyType( )
{
var response = Request.CreateResponse(HttpStatusCode.OK, new MyType[10]);
//Access to header: response.Headers
return response;
}
下面是一个例子:
公共HttpResponseMessage后期产品(产品项)
{
item=存储库。添加(item);
var response=Request.CreateResponse(HttpStatusCode.Created,item);
字符串uri=Url.Link(“DefaultApi”,新的{id=item.id});
response.Headers.Location=新Uri(Uri);
返回响应;
}
以下是一个例子:
公共HttpResponseMessage后期产品(产品项)
{
item=存储库。添加(item);
var response=Request.CreateResponse(HttpStatusCode.Created,item);
字符串uri=Url.Link(“DefaultApi”,新的{id=item.id});
response.Headers.Location=新Uri(Uri);
返回响应;
}
要修改的标题是什么?问什么?问题越具体/详细,得到正确答案的机会就越大,但在一天结束时,头球就是头球。在这种特殊情况下,我对修改位置:
标题感兴趣。您想修改什么标题?问什么?问题越具体/详细,得到正确答案的机会就越大,但在一天结束时,头球就是头球。在本例中,我对修改位置:
标题感兴趣。我使用的是ASP.NET MVC 4。我似乎没有访问HttpContext的权限。至少在ASP.NET MVC 3中可以通过Controller.HttpContext属性访问它-实际上我在MVC 3上编写的项目中使用了它:最初的问题是询问ApicController(WebApi),而不是MVC控制器。因此,你收到了反对票。(不是我说的,只是指出而已)。我使用的是ASP.NETMVC4。我似乎没有访问HttpContext的权限。至少在ASP.NET MVC 3中可以通过Controller.HttpContext属性访问它-实际上我在MVC 3上编写的项目中使用了它:最初的问题是询问ApicController(WebApi),而不是MVC控制器。因此,你收到了反对票。(不是我说的,只是指出而已)。实际情况是,我被传递给函数的是HttpResponseMessage
,因此我不得不使用HttpResponse.CreateContent()
方法。但这为我指明了正确的方向(并回答了我提出的问题!)实际情况是,我正在将HttpResponseMessage
传递给我的函数,因此我必须使用HttpResponse.CreateContent()
方法。但这为我指明了正确的方向(并回答了我提出的问题!)