从Azure WebApp提供JSON静态文件

huangapple go评论80阅读模式
英文:

Serve JSON static files from Azure WebApp

问题

When I try to access JSON files served through the Azure web app, I get a 404 error (The resource you are looking for has been removed, had its name changed, or is temporarily unavailable). Almost every resource I managed to find on the internet suggests adding MIME type for .json extension to the <system.webServer> section of the web.config file, like so:

<staticContent>
	<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>

or somewhere:

<staticContent>
    <remove fileExtension=".json"/>
	<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>

But it does not work for me.

Indeed, when I go to the Kudu console and log the staticContent section with the following command:

%windir%\System32\inetsrv\appcmd list config /section:staticContent

it shows what I assume is the correct MIME type for JSON files (<mimeMap fileExtension=".json" mimeType="application/json" />).

However, trying to access https://appname.azurewebsites.net/some-folder/example.json returns a 404 error. I tried placing other files inside the same directory (txt, css, js, png, font files, etc), and they are all served without any trouble, except for the JSON.

I really don't know how to approach this any further, and I would appreciate some help here.

英文:

When I try to access JSON files served through the Azure web app I get 404 (The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.). Almost every resource I managed to find on the internet suggests adding MIME type for .json extension to the &lt;system.webServer&gt; section of the web.config file, like so:

&lt;staticContent&gt;
	&lt;mimeMap fileExtension=&quot;.json&quot; mimeType=&quot;application/json&quot; /&gt;
&lt;/staticContent&gt;

or somewhere:

&lt;staticContent&gt;
    &lt;remove fileExtension=&quot;.json&quot;/&gt;
	&lt;mimeMap fileExtension=&quot;.json&quot; mimeType=&quot;application/json&quot; /&gt;
&lt;/staticContent&gt;

But it does not work for me.

Indeed, when I go to the Kudu console and log staticContent section with the following command:

%windir%\System32\inetsrv\appcmd list config /section:staticContent

it shows what I assume is the correct MIME type for JSON files (&lt;mimeMap fileExtension=&quot;.json&quot; mimeType=&quot;application/json&quot; /&gt;).

However, trying to access https://appname.azurewebsites.net/some-folder/example.json returns 404. I tried placing other files inside the same directory (txt, css, js, png, font files, etc) and they are all served without any trouble, except for the JSON.

I really don't know how to approach this any further and I would appreciate some help here.

答案1

得分: 1

你需要确保要访问的JSON文件存在于服务器上的正确目录中。尝试清除浏览器缓存并重新加载页面,以查看JSON文件是否正确提供。

另外,你可以启用失败请求跟踪来捕获IIS中的详细错误,它将提供关键的错误和警告,帮助你识别问题。

这是一个参考文档:使用IIS跟踪来排除故障

英文:

You need to make sure that the JSON file you are trying to access exists in the correct directory on the server. Try clearing your browser cache and reloading the page to see if the JSON file is served correctly.

Also, you can enable Failed Request Tracing to capture detailed errors in IIS, it will provide key errors and warnings which will help you identify the problem.

This is a reference document: Troubleshooting Failed Requests Using Tracing in IIS

答案2

得分: 0

我可以为您提供以下翻译部分:

  • Azure App Service上部署时,可以同时从本地和已部署的Azure App Service上提供JSON文件。

请查看以下步骤。

VSStudio中,在wwwroot目录中创建一个新文件夹,并添加.json文件。

从Azure WebApp提供JSON静态文件

  • 将应用程序部署到Azure App Service
  • 确保您的文件夹在KUDU控制台的site\wwwroot\wwwroot文件夹中正确部署。

从Azure WebApp提供JSON静态文件

示例 .json 输出:

从Azure WebApp提供JSON静态文件

英文:

I am able to serve the JSON files both locally and from the deployed Azure App Service.

Check the below steps.

In VSStudio, Create a new folder in the wwwroot directory and add the .json files.

从Azure WebApp提供JSON静态文件

  • Deploy the Application to Azure App Service.
  • Make sure your folder is deployed correctly in the site\wwwroot\wwwroot folder in KUDU Console.

从Azure WebApp提供JSON静态文件

Sample .json Output:

从Azure WebApp提供JSON静态文件

huangapple
  • 本文由 发表于 2023年3月20日 23:31:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定