【发布时间】:2018-06-27 08:29:11
【问题描述】:
我刚刚将我的项目从 ASP.Net 4.5 移至 ASP.Net Core。 我有一个用于返回 blob 但现在返回 JSON 的 REST API。
这是旧代码:
[HttpGet]
[ResponseType(typeof(HttpResponseMessage))]
[Route("Download/{documentId}")]
public async Task<HttpResponseMessage> DownloadDocument(string documentId)
{
try
{
var result = await TheDocumentService.DownloadDocument(documentId);
return result;
}
catch (Exception ex)
{
return new HttpResponseMessage
{
StatusCode = HttpStatusCode.InternalServerError,
Content = new StringContent(ex.Message)
};
}
}
除了[ResponseType(typeof(HttpResponseMessage))] 在 ASP.Net Core 中不起作用之外,ASP.net Core 中的代码相同,两种解决方案的返回结果也相同。
但是当在客户端查看来自服务器的响应时,它们会有所不同。
所以它们之间唯一不同的是[ResponseType(typeof(HttpResponseMessage))]。 asp.net core 中有没有等价的东西?
【问题讨论】:
-
你的控制器顶部有 [Produces("application/json")] 吗?
-
@SBFrancies nope only
[HttpGet] [Route("Download/{documentId}")] -
我的意思是控制器本身,而不是动作。
-
@SBFrancies 不只是一条路线。
-
这个问题的答案可能会对你有所帮助:stackoverflow.com/questions/42460198/…
标签: c# asp.net asp.net-mvc asp.net-core