MVC5中的WWWROOT [英] WWWROOT in MVC5

查看:207
本文介绍了MVC5中的WWWROOT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to achieve same behavior in ASP.NET MVC5 with static files like it works on aspnet-core with app.UseDefaultFiles(); app.UseStaticFiles();?

我的意思是从根目录的某个文件夹中提供静态文件,例如必须在mysite.com/some.html上打开/wwwroot/some.html,在mysite.com/img/test.jpg上打开/wwwroot/img/test.jpg等.

I mean serving static files from some folder over root, e.g. /wwwroot/some.html must be opened on mysite.com/some.html, /wwwroot/img/test.jpg on mysite.com/img/test.jpg etc.

更新:我创建了wwwroot文件夹,并将以下规则添加到web.config:

Update: I have created wwwroot folder and added following rule to 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的网址效果很好.
我在做什么错了?

So IIS must return files from wwwroot except when calls go to /api/something, but I'm always getting index.html in wwwroot folder and never other files. Api's URL works good.
What I'm doing wrong?

推荐答案

所有工作方式均如此:

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

别忘了安装重写模块.

这篇关于MVC5中的WWWROOT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆