WWWROOT在MVC5中

问题描述 投票:5回答:1

如何在ASP.NET MVC5中使用静态文件实现相同的行为,就像它在app.UseDefaultFiles(); app.UseStaticFiles();的aspnet-core上一样?

我的意思是从root上的某个文件夹提供静态文件,例如/wwwroot/some.html必须在mysite.com/some.html/wwwroot/img/test.jpgmysite.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,但我总是在index.html文件夹中获取wwwroot,而不是其他文件。 Api的URL工作得很好。 我做错了什么?

asp.net-mvc iis asp.net-mvc-5 url-rewriting static-files
1个回答
1
投票

一切都以这种方式奏效:

<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>

别忘了install rewrite module

© www.soinside.com 2019 - 2024. All rights reserved.