我有一个ExecuteAsync
&#34; old&#34; ASP.NET,具有 public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = _value == null ?
_request.CreateResponse(StatusCode) :
_request.CreateResponse(StatusCode, _value);
if (_uri != null)
{
var relativeUri = new Uri(_uri.PathAndQuery, UriKind.Relative);
response.Headers.Location = relativeUri;
}
return Task.FromResult(response);
}
的以下代码:
IActionResult
现在,要将其转换为ASP.NET Core public async Task ExecuteResultAsync(ActionContext context)
,我意识到我需要将签名更改为
_value
我有两个具体的需求:
答案 0 :(得分:2)
是的,你绝对可以在这种情况下使用ObjectResponse
。
关于标题:在ExecuteResultAsync
方法中,您可以通过context.HttpContext.Response
属性直接修改响应,所以请执行以下操作:
public async Task ExecuteResultAsync(ActionContext context)
{
// note: ObjectResult allows to pass null in ctor
var objectResult = new ObjectResult(_value)
{
StatusCode = <needed status_code>
};
context.HttpContext.Response.Headers["Location"] = "<your relativeUri>";
await objectResult.ExecuteResultAsync(context);
}