我正在尝试创建一个结合了ASP.NET WebAPI和Yeoman生成器的单页web应用程序。目前我有一个项目结构,如下所示
|-- yeomanAngularApp |-- app |-- dist |-- scripts |-- index.html |-- etc... |-- WebApiApp |-- App_Start |-- bin |-- Content |-- Controllers |-- Models |-- WebApiApp.csproj |-- dist |-- scripts |-- index.html
当我想要构建应用程序分发版时,我将yeomanAngularApp
中的dist
文件夹复制到WebApiApp
中,以替换其中的dist
文件夹。
现在,这一切都很容易做到。然后,我真正想要做的是告诉WebApiApp
不要使用WebApiApp\
作为项目的根目录,而要使用WebApiApp\dist
。这意味着,即使index.html
在dist
文件夹中,我也可以转到http://localhost/index.html
,而不是转到dist
。最重要的是,我也希望我的控制器的WebAPI路由也能很好地运行。
我已经找了一段时间了,但我似乎找不到答案。我能想到的最好的办法就是使用URL重写,这对我来说是不正确的。
发布于 2014-07-19 03:26:36
URL重写正是您想要的,使用一个条件块来测试文件是否存在。dist
目录下的内容是静态的(即驻留在文件系统上),但是WebApiApp
路由是动态的。因此,您只需测试路由是否与dist
目录中存在的文件匹配,如果不匹配,则只需让.NET处理路由。将以下内容添加到<system.webServer>
部分中的Web.config
文件中应该可以解决这个问题:
<rewrite>
<rules>
<rule name="static dist files" stopProcessing="true">
<match url="^(.+)$" />
<conditions>
<add input="{APPL_PHYSICAL_PATH}dist\{R:1}" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="/dist/{R:1}" />
</rule>
<rule name="index.html as document root" stopProcessing="true">
<match url="^$" />
<action type="Rewrite" url="/dist/index.html" />
</rule>
</rules>
</rewrite>
第二个规则是可选的,但它意味着对站点根目录的请求仍将服务于dist
目录中的index.html
文件,有效地使项目的根成为WebApiApp\dist
,但仍然允许所有WebAPI路由。
https://stackoverflow.com/questions/24780087
复制相似问题