简体   繁体   中英

ASP.NET-MVC serve files in a directory as if they were in root

I have an ASP.NET-MVC5 project, the client is a Single Page Application written in AngularJS (only static files needed to launch it, C# Controllers don't serve HTML, they only process AJAX calls). For convenience, all the static files for the Angular part (.html, .js, .css) are in the WebClient directory located in the root.

Everything works fine with paths like www.my-app.com/webclient/index.html , but I would like it to work with just www.my-app.com/index.html . For that I would somehow need to map the WebClient directory to the root address. What is the best way to do this? Preferably, with just Web.config file (but any working way is welcome).

PS I know that I can write my own controller to do this, but, as far as I understand, it will be slower than built in StaticFileHandler. I'm trying to find a better solution. Thanks in advance.

Okey, so, as always, the guaranteed way to encounter the answer in the next 5 minutes is to finally ask the question on SO.

The answer is IIS URL Rewrite Module. Found the info on it here

You need to add this to your Web.config (the regexp still needs some work to shield the API route and add all possible symbols)

  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Route root to WebClient" stopProcessing="true">
            <match url="^([_0-9a-z-./\?]+)" />
            <action type="Rewrite" url="WebClient/{R:1}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM