如何在ASP.NET MVC5中使用类似于aspnet核的静态文件和app.UseDefaultFiles(); app.UseStaticFiles();实现相同的行为
我的意思是从某个文件夹通过根提供静态文件,例如/wwwroot/some.html必须在mysite.com/some.html上打开,/wwwroot/img/test.jpg必须在mysite.com/img/test.jpg上打开等等。
更新:我创建了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,但我总是在wwwroot文件夹中获取index.html,而不是其他文件。Api的URL运行良好。
我做错什么了?
发布于 2019-04-03 11:00:57
所有的工作都是这样的:
<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://stackoverflow.com/questions/55488381
复制相似问题