如何使用静态文件在 ASP.NET MVC5 中实现相同的行为,就像在 aspnet-core 上一样app.UseDefaultFiles(); app.UseStaticFiles();
?
我的意思是通过根目录从某个文件夹提供静态文件,例如/wwwroot/some.html
必须打开于mysite.com/some.html
, /wwwroot/img/test.jpg
on mysite.com/img/test.jpg
etc.
Update: 我创造了wwwroot
文件夹并将以下规则添加到web.config
:
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite Static" stopProcessing="true">
<match url="^(?!(wwwroot/|api/))(.*)$" ignoreCase="true"></match>
<action type="Rewrite" url="/wwwroot/{R:1}" />
</rule>
</rules>
所以 IIS 必须返回文件wwwroot
除非呼叫转至/api/something
,但我总是得到index.html
in wwwroot
文件夹,绝不是其他文件。 Api 的 URL 运行良好。
我做错了什么?
一切都以这种方式进行:
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite Static" stopProcessing="true">
<match url="^((?!(wwwroot\/|api\/))(.*))$" ignoreCase="true"></match>
<action type="Rewrite" url="/wwwroot/{R:1}" />
</rule>
</rules>
</rewrite>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<remove name="StaticFile"/>
<add
name="StaticFile"
path="*" verb="*"
modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule"
resourceType="Either"
requireAccess="Read" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
</handlers>
<staticContent>
<mimeMap fileExtension=".*" mimeType="application/octet-stream" />
</staticContent>
<modules>
<remove name="TelemetryCorrelationHttpModule" />
<add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler" />
</modules>
</system.webServer>
别忘了安装重写模块 https://www.iis.net/downloads/microsoft/url-rewrite.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)