c# - 如何在.NET core 2.0服务中设置响应头

标签 c# http-headers .net-core

我有一个 dotnet core 2 服务,我想在发送响应之前添加响应 header ,但我不知道该怎么做。 header 的值取决于请求,并非所有请求都相同。我想添加的示例端点如下。

[HttpPost("MyEndpoint")]
public string MyEndpoint([FromBody][Required] RequestBody requestBody)
{
    string responseBody = DoStuff(requestBody);

    // How to set response header?

    return responseBody;
}

附:抱歉,我的用户名很愚蠢,我当时急于创建帐户,但现在 30(25 个以上)天内无法更改它。

最佳答案

试试这个:

[HttpPost("MyEndpoint")]
public string MyEndpoint([FromBody][Required] RequestBody requestBody)
{
    string responseBody = DoStuff(requestBody);

    // How to set response header?
    this.Response.Headers.Add("MyHeader", "MyHeaderValue");

    return responseBody;
}

关于c# - 如何在.NET core 2.0服务中设置响应头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50630398/

相关文章:

ios - Alamofire 在接收所有数据之前验证 header (完整主体)

ruby-on-rails - 在 RoR : Which one to use out of redirect_to and head :moved_permanently? 中重定向

linux - 无法在某些目录下运行 dotnet?

c# - 如何等待C#中的CreateProcessAsUser完成

c# - 反序列化为通用排序列表 C#

c# - 如何使用依赖注入(inject)实现通用存储库

apache - 如何防止 Apache httpd (MAMP) 中的 http 文件缓存

.net-core - VSTS 版本 : Packages failed to restore - Unable to resolve 'NETStandard.Library (>= 1.6.1)' for '.NETStandard,Version=v2.0'

c# - 如何去除多个 if-else 或重复的三元运算?

C#:要实现 XML 序列化,从 IEnumerable 继承的类型必须具有 Add(System.Object) 的实现