我正在寻找在我的. net核心Web API控制器中返回带有HTTP状态代码的JSON的正确方法。我以前是这样用的:

public IHttpActionResult GetResourceData()
{
    return this.Content(HttpStatusCode.OK, new { response = "Hello"});
}

这是在一个4.6 MVC应用程序,但现在与。net核心,我似乎没有这个IHttpActionResult,我有ActionResult和使用像这样:

public ActionResult IsAuthenticated()
{
    return Ok(Json("123"));
}

但是服务器的反应很奇怪,如下图所示:

我只是想让Web API控制器返回带有HTTP状态码的JSON,就像我在Web API 2中所做的那样。


使用JsonResult响应的最基本版本是:

// GET: api/authors
[HttpGet]
public JsonResult Get()
{
    return Json(_authorRepository.List());
}

但是,这对您的问题没有帮助,因为您不能显式地处理自己的响应代码。

控制状态结果的方法是,你需要返回一个ActionResult,在那里你可以利用StatusCodeResult类型。

例如:

// GET: api/authors/search?namelike=foo
[HttpGet("Search")]
public IActionResult Search(string namelike)
{
    var result = _authorRepository.GetByNameSubstring(namelike);
    if (!result.Any())
    {
        return NotFound(namelike);
    }
    return Ok(result);
}

注意以上两个例子都来自微软文档:格式化响应数据


额外的东西

我经常遇到的问题是,我想对我的WebAPI进行更细粒度的控制,而不是仅仅使用VS中的“新项目”模板的默认配置。

让我们来确保你掌握了一些基本知识……

步骤1:配置服务

为了得到你的ASP。NET核心WebAPI响应一个JSON序列化对象以及完全控制状态代码,你应该首先确保你在你的ConfigureServices方法中包含了AddMvc()服务,通常在Startup.cs中找到。

需要注意的是,addmvc()将自动包括JSON的输入/输出格式化器以及对其他请求类型的响应。

如果你的项目需要完全控制,你想严格定义你的服务,比如你的WebAPI如何对各种请求类型(包括application/json)进行行为,而不响应其他请求类型(比如标准的浏览器请求),你可以用下面的代码手动定义它:

public void ConfigureServices(IServiceCollection services)
{
    // Build a customized MVC implementation, without using the default AddMvc(), instead use AddMvcCore().
    // https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc/MvcServiceCollectionExtensions.cs

    services
        .AddMvcCore(options =>
        {
            options.RequireHttpsPermanent = true; // does not affect api requests
            options.RespectBrowserAcceptHeader = true; // false by default
            //options.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();

            //remove these two below, but added so you know where to place them...
            options.OutputFormatters.Add(new YourCustomOutputFormatter()); 
            options.InputFormatters.Add(new YourCustomInputFormatter());
        })
        //.AddApiExplorer()
        //.AddAuthorization()
        .AddFormatterMappings()
        //.AddCacheTagHelper()
        //.AddDataAnnotations()
        //.AddCors()
        .AddJsonFormatters(); // JSON, or you can build your own custom one (above)
}

您将注意到,我还提供了一种添加您自己的自定义输入/输出格式化器的方法,以便您可能希望响应另一种序列化格式(protobuf、thrift等)。

上面的代码块主要是AddMvc()方法的副本。但是,我们通过定义每个服务来实现每个“默认”服务,而不是使用预先发布的模板。我已经在代码块中添加了存储库链接,或者你可以从GitHub存储库中查看AddMvc() ..

请注意,有一些指南将试图通过“撤消”默认值来解决这个问题,而不是一开始就不实现它……如果你考虑到我们现在正在与开源合作,这是冗余的工作,糟糕的代码,坦率地说,这是一个很快就会消失的旧习惯。


步骤2:创建Controller

我要给你们看一个非常简单的例子来解决你们的问题。

public class FooController
{
    [HttpPost]
    public async Task<IActionResult> Create([FromBody] Object item)
    {
        if (item == null) return BadRequest();

        var newItem = new Object(); // create the object to return
        if (newItem != null) return Ok(newItem);

        else return NotFound();
    }
}

步骤3:检查你的内容类型和接受

您需要确保请求中的Content-Type和Accept标头设置正确。在您的示例中(JSON),您将希望将其设置为application/ JSON。

如果你想让你的WebAPI默认响应JSON,不管请求头指定什么,你可以通过几种方式来做到这一点。

方法1 正如我之前推荐的文章(格式化响应数据)所示,您可以在控制器/动作级别强制使用特定的格式。我个人不喜欢这种方法……但为了完整起见,这里是:

Forcing a Particular Format If you would like to restrict the response formats for a specific action you can, you can apply the [Produces] filter. The [Produces] filter specifies the response formats for a specific action (or controller). Like most Filters, this can be applied at the action, controller, or global scope. [Produces("application/json")] public class AuthorsController The [Produces] filter will force all actions within the AuthorsController to return JSON-formatted responses, even if other formatters were configured for the application and the client provided an Accept header requesting a different, available format.

方法2 我的首选方法是让WebAPI以所请求的格式响应所有请求。然而,如果它不接受所请求的格式,那么就会退回到默认格式(例如。JSON)

首先,您需要在选项中注册它(如前所述,我们需要重做默认行为)

options.RespectBrowserAcceptHeader = true; // false by default

最后,通过简单地重新排序在服务构建器中定义的格式化器列表,web主机将默认为您定位在列表顶部的格式化器(即位置0)。

更多信息可以在.NET Web开发和工具博客中找到


您已经为大多数常见的状态代码预定义了方法。

result返回200 CreatedAtRoute返回201 +新的资源URL NotFound返回404 BadRequest返回400等等。

所有方法的列表请参见baseconcontroller .cs和Controller.cs。

但如果你真的坚持你可以使用StatusCode来设置一个自定义代码,但你真的不应该这样做,因为它使代码可读性较差,你将不得不重复代码来设置头部(如CreatedAtRoute)。

public ActionResult IsAuthenticated()
{
    return StatusCode(200, "123");
}

请参考以下代码,您可以管理多个状态代码与不同类型的JSON

public async Task<HttpResponseMessage> GetAsync()
{
    try
    {
        using (var entities = new DbEntities())
        {
            var resourceModelList = entities.Resources.Select(r=> new ResourceModel{Build Your Resource Model}).ToList();

            if (resourceModelList.Count == 0)
            {
                return this.Request.CreateResponse<string>(HttpStatusCode.NotFound, "No resources found.");
            }

            return this.Request.CreateResponse<List<ResourceModel>>(HttpStatusCode.OK, resourceModelList, "application/json");
        }
    }
    catch (Exception ex)
    {
        return this.Request.CreateResponse<string>(HttpStatusCode.InternalServerError, "Something went wrong.");
    }
}

我想到的最简单的方法是:

var result = new Item { Id = 123, Name = "Hero" };

return new JsonResult(result)
{
    StatusCode = StatusCodes.Status201Created // Status code here 
};

ASP。NET Core 2.0,从Web API(与MVC统一并使用相同的基类Controller)返回对象的理想方式是

public IActionResult Get()
{
    return new OkObjectResult(new Item { Id = 123, Name = "Hero" });
}

请注意,

它返回200个OK状态码(这是ObjectResult的OK类型) 它进行内容协商,即它将根据请求中的Accept报头返回。如果在请求中发送Accept: application/xml,它将以xml的形式返回。如果不发送任何内容,则默认为JSON。

如果需要发送特定的状态代码,则使用ObjectResult或StatusCode。两者都做同样的事情,并支持内容协商。

return new ObjectResult(new Item { Id = 123, Name = "Hero" }) { StatusCode = 200 };
return StatusCode( 200, new Item { Id = 123, Name = "Hero" });

或者更细粒度的ObjectResult:

 Microsoft.AspNetCore.Mvc.Formatters.MediaTypeCollection myContentTypes = new Microsoft.AspNetCore.Mvc.Formatters.MediaTypeCollection { System.Net.Mime.MediaTypeNames.Application.Json };
 String hardCodedJson = "{\"Id\":\"123\",\"DateOfRegistration\":\"2012-10-21T00:00:00+05:30\",\"Status\":0}";
 return new ObjectResult(hardCodedJson) { StatusCode = 200, ContentTypes = myContentTypes };

如果您特别希望以JSON形式返回,有几种方法

//GET http://example.com/api/test/asjson
[HttpGet("AsJson")]
public JsonResult GetAsJson()
{
    return Json(new Item { Id = 123, Name = "Hero" });
}

//GET http://example.com/api/test/withproduces
[HttpGet("WithProduces")]
[Produces("application/json")]
public Item GetWithProduces()
{
    return new Item { Id = 123, Name = "Hero" };
}

请注意,

两者都以两种不同的方式强制执行JSON。 两者都忽略了内容协商。 第一种方法使用特定的序列化器JSON (object)强制执行JSON。 第二种方法通过使用products()属性(这是一个ResultFilter)和contentType = application/json来实现同样的功能

在官方文档中阅读更多关于它们的信息。点击这里了解过滤器。

示例中使用的简单模型类

public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
}

这是我最简单的解决方案:

public IActionResult InfoTag()
{
    return Ok(new {name = "Fabio", age = 42, gender = "M"});
}

or

public IActionResult InfoTag()
{
    return Json(new {name = "Fabio", age = 42, gender = "M"});
}

而不是使用404/201状态代码使用enum

     public async Task<IActionResult> Login(string email, string password)
    {
        if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password))
        { 
            return StatusCode((int)HttpStatusCode.BadRequest, Json("email or password is null")); 
        }

        var user = await _userManager.FindByEmailAsync(email);
        if (user == null)
        {
            return StatusCode((int)HttpStatusCode.BadRequest, Json("Invalid Login and/or password"));

        }
        var passwordSignInResult = await _signInManager.PasswordSignInAsync(user, password, isPersistent: true, lockoutOnFailure: false);
        if (!passwordSignInResult.Succeeded)
        {
            return StatusCode((int)HttpStatusCode.BadRequest, Json("Invalid Login and/or password"));
        }
        return StatusCode((int)HttpStatusCode.OK, Json("Sucess !!!"));
    }

我在我的Asp Net Core Api应用程序中所做的是创建一个从ObjectResult扩展的类,并提供许多构造函数来定制内容和状态代码。 然后我所有的控制器动作都使用适当的构造函数之一。 你可以看看我的实现: https://github.com/melardev/AspNetCoreApiPaginatedCrud

and

https://github.com/melardev/ApiAspCoreEcommerce

下面是类的样子(去我的回购完整代码):

public class StatusCodeAndDtoWrapper : ObjectResult
{



    public StatusCodeAndDtoWrapper(AppResponse dto, int statusCode = 200) : base(dto)
    {
        StatusCode = statusCode;
    }

    private StatusCodeAndDtoWrapper(AppResponse dto, int statusCode, string message) : base(dto)
    {
        StatusCode = statusCode;
        if (dto.FullMessages == null)
            dto.FullMessages = new List<string>(1);
        dto.FullMessages.Add(message);
    }

    private StatusCodeAndDtoWrapper(AppResponse dto, int statusCode, ICollection<string> messages) : base(dto)
    {
        StatusCode = statusCode;
        dto.FullMessages = messages;
    }
}

注意用对象替换dto的基数(dto)应该没问题了。


我在这里找到了很棒的答案,我还尝试了这个返回语句,见StatusCode(任何你想要的代码),它工作了!!

return Ok(new {
                    Token = new JwtSecurityTokenHandler().WriteToken(token),
                    Expiration = token.ValidTo,
                    username = user.FullName,
                    StatusCode = StatusCode(200)
                });

我要用这个。我的大问题是我的json是一个字符串(在我的数据库…而不是特定的/已知的类型)。

好吧,我终于把它弄好了。

////[Route("api/[controller]")]
////[ApiController]
////public class MyController: Microsoft.AspNetCore.Mvc.ControllerBase
////{
                    //// public IActionResult MyMethod(string myParam) {

                    string hardCodedJson = "{}";
                    int hardCodedStatusCode = 200;

                    Newtonsoft.Json.Linq.JObject job = Newtonsoft.Json.Linq.JObject.Parse(hardCodedJson);
                    /* "this" comes from your class being a subclass of Microsoft.AspNetCore.Mvc.ControllerBase */
                    Microsoft.AspNetCore.Mvc.ContentResult contRes = this.Content(job.ToString());
                    contRes.StatusCode = hardCodedStatusCode;

                    return contRes;

                    //// } ////end MyMethod
              //// } ////end class

我恰巧用的是asp.net core 3.1

#region Assembly Microsoft.AspNetCore.Mvc.Core, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
//C:\Program Files\dotnet\packs\Microsoft.AspNetCore.App.Ref\3.1.0\ref\netcoreapp3.1\Microsoft.AspNetCore.Mvc.Core.dll

我从这里得到了提示::https://www.jianshu.com/p/7b3e92c42b61


控制器动作返回类型在ASP。NET核心web API 02/03/2020

6分钟阅读 + 2

作者:斯科特·艾迪·林克

同步动作

[HttpGet("{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult<Product> GetById(int id)
{
    if (!_repository.TryGetProduct(id, out var product))
    {
        return NotFound();
    }

    return product;
}

异步操作

[HttpPost]
[Consumes(MediaTypeNames.Application.Json)]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<Product>> CreateAsync(Product product)
{
    if (product.Description.Contains("XYZ Widget"))
    {
        return BadRequest();
    }

    await _repository.AddProductAsync(product);

    return CreatedAtAction(nameof(GetById), new { id = product.Id }, product);
}

我发现的最干净的解决方案是在Startup.cs中的ConfigureServices方法中设置以下内容(在我的情况下,我希望剥离TZ信息。我总是希望看到用户看到的日期时间)。

   services.AddControllers()
                .AddNewtonsoftJson(o =>
                {
                    o.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Unspecified;
                });

DateTimeZoneHandling选项有Utc、Unspecified、Local或RoundtripKind

我仍然想找到一种方法,能够在每个呼叫的基础上请求这一点。

类似的

  static readonly JsonMediaTypeFormatter _jsonFormatter = new JsonMediaTypeFormatter();
 _jsonFormatter.SerializerSettings = new JsonSerializerSettings()
                {DateTimeZoneHandling = DateTimeZoneHandling.Unspecified};

return Ok("Hello World", _jsonFormatter );

我从ASP转换。NET,在那里我使用了下面的helper方法

public static ActionResult<T> Ok<T>(T result, HttpContext context)
    {
        var responseMessage = context.GetHttpRequestMessage().CreateResponse(HttpStatusCode.OK, result, _jsonFormatter);
        return new ResponseMessageResult(responseMessage);
    }