简体   繁体   English

如何将此IHttpActionResult转换为ASP.NET Core IActionResult

[英]How to translate this IHttpActionResult to ASP.NET Core IActionResult

I've got an IHttpActionResult in "old" ASP.NET, which has the following code for ExecuteAsync : 我在“旧的” ASP.NET中有一个IHttpActionResult ,它具有ExecuteAsync的以下代码:

    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);
    }

Now, to translate this to the ASP.NET Core IActionResult , I realize that I need to change the signature to 现在,要将其转换为ASP.NET Core IActionResult ,我意识到我需要将签名更改为

    public async Task ExecuteResultAsync(ActionContext context)

I have two specific needs: 我有两个特定需求:

  1. If _value is not null, send it back as the body of the response; 如果_value不为null,则将其作为响应的正文发送回; and
  2. If _uri is not null, send it back in the Location header of the response. 如果_uri不为null,则将其发送回响应的Location标头中。

I've found lots of similar questions, like this , which suggests manipulating the context 's Response property - but this doesn't answer how to set the body for an object (rather than a string), or this , which suggests using an ObjectResponse , but it isn't clear to me how I would add the required header. 我发现了很多类似的问题,例如this ,它建议操作contextResponse属性-但这不能回答如何为object (而不是字符串)设置主体,或者this ,它建议使用ObjectResponse ,但是我不清楚如何添加所需的标头。

How do you do it? 你怎么做呢?

Yes, you definitely could use ObjectResponse for this case. 是的,您肯定可以在这种情况下使用ObjectResponse

Regarding headers: in ExecuteResultAsync method you can directly modify the Response via context.HttpContext.Response property, so do something like this: 关于标题:在ExecuteResultAsync方法中,您可以通过context.HttpContext.Response属性直接修改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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 ASP.NET Core 中返回 403 Forbidden 响应作为 IActionResult - How to return 403 Forbidden response as IActionResult in ASP.NET Core 从ASP.NET Core中的WebSocket请求返回什么IActionResult? - What IActionResult to return from a WebSocket request in ASP.NET Core? C# 接口概念说明与 ASP.NET Core 中的 IActionResult - C# interface concept clarification with IActionResult in ASP.NET Core 数据表 asp.net 核心剃刀页面 IActionResult 新 JsonResult - datatables asp.net core razor pages IActionResult new JsonResult Asp.net core IActionResult csv 编码问题 - ASp.net core IActionResult csv encoding problem 如何在ASP.NET Core中返回自定义HTTP状态/消息而不返回对象,IActionResult等? - How to return a custom HTTP status/message in ASP.NET Core without returning object, IActionResult, etc? 如何测试我的 ASP.NET Core 2.2 Web API GET IActionResult 返回 Ok(object)? - How to test my ASP.NET Core 2.2 Web API GET IActionResult that returns Ok(object)? ASP.Net WebAPI:如何使用IHttpActionResult而不是HttpResponseMessage - ASP.Net WebAPI: How to use IHttpActionResult instead of HttpResponseMessage 如何在 IActionResult Asp.net WebAPI 中传递多个参数(对象) - How to pass multiple parameters (Objects) in IActionResult Asp.net WebAPI 返回“JsonResult / IActionResult”或“Task”有什么不同 <SomeObject> “或者只是Asp.net Core Web API中的”SomeObject“? - What is the difference in returning “JsonResult/IActionResult” or “Task<SomeObject>” or just “SomeObject” in a Asp.net Core Web API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM