简体   繁体   English

ASP.NET-MVC将目录中的文件视为根目录中的文件

[英]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). 我有一个ASP.NET-MVC5项目,客户端是用AngularJS编写的单页应用程序(只有静态文件才能启动它,C#控制器不提供HTML,它们仅处理AJAX调用)。 For convenience, all the static files for the Angular part (.html, .js, .css) are in the WebClient directory located in the root. 为了方便起见,Angular部分的所有静态文件(.html,.js,.css)都位于根目录中的WebClient目录中。

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 . 一切都可以与www.my-app.com/webclient/index.html路径www.my-app.com/webclient/index.html ,但我希望它仅与www.my-app.com/index.html一起工作。 For that I would somehow need to map the WebClient directory to the root address. 为此,我需要以某种方式将WebClient目录映射到根地址。 What is the best way to do this? 做这个的最好方式是什么? Preferably, with just Web.config file (but any working way is welcome). 最好仅使用Web.config文件(但欢迎任何工作方式)。

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. PS我知道我可以编写自己的控制器来执行此操作,但是据我了解,它会比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. Okey因此,与往常一样,在接下来的5分钟内遇到问题的肯定方法是最终提出关于SO的问题。

The answer is IIS URL Rewrite Module. 答案是IIS URL重写模块。 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) 您需要将其添加到Web.config中(regexp仍需要做一些工作来屏蔽API路由并添加所有可能的符号)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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