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

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

ASP.NET Framework 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);
}


作者最新文章
C# 使用 CSVHelper 操作 csv 文件, .net core, .net framework 读取写入 csv 文件
C# 实现字符串文本换行的方法,文本如何换行
C# 如何循环读取文件每一行文本内容
C# DateTime AddMonths 的错误用法导致跳过日期
C# 全角转换半角,半角转换为全角