【发布时间】:2021-04-09 14:35:45
【问题描述】:
我已将我的全栈解决方案部署到 Azure,其中包含 Blazor 中的前端和在 ASP .NET Core 中创建的 REST API。所有端点都可以工作,除了一个端点是GET api/shows。当我尝试通过 Postman 或直接在 Azure 中调用它时,它可以正常工作,但是当我的前端尝试调用它时,它会返回索引页面的 HTML。
我昨天设法让它运行起来,但现在我发布了一个新版本的前端,它又不能正常工作了。看起来 Azure 没有设置请求标头,因为 api/shows 的标头有 Content-Type: text/html,但在 Azure 中我将标头设置为 Content-Type: application/json。
在 Azure 中,我将入站和出站策略设置为:
<policies>
<inbound>
<base />
<set-header name="Accept" exists-action="append">
<value>application/json</value>
</set-header>
<set-header name="Content-Type" exists-action="append">
<value>application/json</value>
</set-header>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-header name="Accept" exists-action="override">
<value>application/json</value>
</set-header>
</outbound>
<on-error>
<base />
</on-error>
</policies>
这些是GET api/shows的响应头
Accept-Ranges: bytes
Content-Encoding: gzip
Content-Length: 1401
Content-Type: text/html
Date: Sun, 03 Jan 2021 15:41:58 GMT
ETag: "652521af3d0d61:0"
Last-Modified: Sun, 13 Dec 2020 01:55:31 GMT
Server: Microsoft-IIS/10.0
Vary: Accept-Encoding
X-Powered-By: ASP.NET
而其他请求,f.e.按日期获取节目GET api/shows/02-01-2021 有
Content-Encoding: gzip
Content-Length: 122
Content-Type: application/json; charset=utf-8
Date: Sun, 03 Jan 2021 15:55:19 GMT
Server: Microsoft-IIS/10.0
Strict-Transport-Security: max-age=2592000
Vary: Accept-Encoding
X-Powered-By: ASP.NET
在 Shows 控制器中:
[HttpGet]
[Produces("application/json")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<IEnumerable<MovieShow>>> Get()
{
var shows = await _mediator.Send(new GetShowsQuery());
return Ok(shows);
}
编辑:为了好玩,我尝试在浏览器中重新启用缓存。突然就可以正常使用了。
【问题讨论】:
-
" 然后它返回索引页面的 HTML" 我猜代词 "it" 指的是 Web Api,对吧? Web Api 如何访问索引页面 html ?是否显示索引页面(实际上是索引组件)而不是您尝试执行 Web Api 调用的页面?根据我的经验(不是个人经验,而是通过阅读 SO 中的问题收集的),SYSTEM.NET.HTTP.JSON 在 Blazor 中引发了一个异常,因为它无法反序列化响应数据,因为它是 Html 而不是预期的 JSON.. .这是你的问题吗?谢谢...
标签: azure api rest asp.net-core blazor