ASP.NET WebApi 与 ASP.NET Core WebApi 返回 HttpResponseMessage

ASP.NET WebApi 与 ASP.NET Core WebApi 返回 HttpResponseMessage

ASP.NET WebApi

[Route("values/{id}")]
public async Task<HttpResponseMessage> Get(string id)
{
    var response = Request.CreateResponse(HttpStatusCode.OK);
    var accept = Request.Headers.Accept;
    var result = await _valuesService.Get(id);

    if (accept.Any(x => x.MediaType == "text/html"))
    {
        response.Content = new StringContent(result, Encoding.UTF8, "text/html");
    }
    else
    {
        response.Content = new StringContent(result, Encoding.UTF8, "text/plain");
    }
    return response;
}


ASP.NET Core WebApi
[Route("values/{id}")]
public async Task Get(string id)
{
    var accept = Request.GetTypedHeaders().Accept;
    var result = await _valuesService.Get(id);
    var data = Encoding.UTF8.GetBytes(result);
    if (accept.Any(x => x.MediaType == "text/html"))
    {
        Response.ContentType = "text/html";
    }
    else
    {
        Response.ContentType = "text/plain";
    }
    await Response.Body.WriteAsync(data, 0, data.Length);
}


作者最新文章
ASP.NET, ASP.NET Core WebApi 返回 Controller 返回 Json 类型结果
Visual Studio 调试 ASP.NET Core 无法连接 IIS Express 服务器
ASP.NET WebApi 与 ASP.NET Core WebApi 返回 HttpResponseMessage
SQL Server 中 CASE WHEN 使用
html table 设置表格背景图片,背景色