Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WWWROOT in MVC5

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

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.

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>

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?

like image 248
Troll the Legacy Avatar asked Apr 03 '19 06:04

Troll the Legacy


People also ask

What is the Wwwroot folder?

By default, the wwwroot folder in the ASP.NET Core project is treated as a web root folder. Static files can be stored in any folder under the web root and accessed with a relative path to that root.

How do I add Wwwroot folder to Web API?

In order to add the wwwroot folder, right-click on the project and then select add => new folder option and then provide the folder name as wwwroot.

Where do I put static files in NET Core?

Static files are stored within the project's web root directory. The default directory is {content root}/wwwroot , but it can be changed with the UseWebRoot method. For more information, see Content root and Web root.


1 Answers

All works in that way:

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

Don't forget to install rewrite module.

like image 180
Troll the Legacy Avatar answered Nov 15 '22 04:11

Troll the Legacy