I am trying create a single page web app combining both ASP.NET WebAPI and the Yeoman Angluarjs generator. Currently I have a project structure as laid out below
我正在尝试创建一个结合ASP.NET WebAPI和Yeoman Angluarjs生成器的单页Web应用程序。目前我的项目结构如下所示
|-- yeomanAngularApp |-- app |-- dist |-- scripts |-- index.html |-- etc... |-- WebApiApp |-- App_Start |-- bin |-- Content |-- Controllers |-- Models |-- WebApiApp.csproj |-- dist |-- scripts |-- index.html
| - yeomanAngularApp | - app | - dist | - scripts | - index.html | - etc ... | - WebApiApp | - App_Start | - bin | - 内容| - 控制器| - 模型| - WebApiApp.csproj | - dist | - scripts | - index.html
When I want to build the app distribution, I copy the dist
folder in the yeomanAngularApp
into the WebApiApp
replacing the dist
folder in there.
当我想构建应用程序发行版时,我将yeomanAngularApp中的dist文件夹复制到WebApiApp中,替换那里的dist文件夹。
Now this is all very easy to do. What I really then want to do is to tell the WebApiApp
not to use WebApiApp\
as the root of the project, but use WebApiApp\dist
. This means instead of going to http://localhost/dist/index.html
, I could go to http://localhost/index.html
even though index.html
is in the dist
folder. On top of this I would also like my WebAPI routing for the controllers to play nicely too.
现在这很容易做到。我当时想要做的是告诉WebApiApp不要使用WebApiApp \作为项目的根目录,而是使用WebApiApp \ dist。这意味着我可以转到http://localhost/index.html而不是转到http://localhost/dist/index.html,即使index.html位于dist文件夹中。除此之外,我还希望控制器的WebAPI路由能够很好地发挥作用。
I've been searching for a while and I can't seem to find an answer. The best I can come up with, is using URL rewriting, which to me doesn't feel right.
我一直在寻找,我似乎无法找到答案。我能想到的最好的就是使用URL重写,这对我来说感觉不对。
1 个解决方案
#1
24
URL rewriting is exactly what you want, with a condition block to test if the file exists. The content under your dist
directory is static (i.e. lives on the file system) but the WebApiApp
routes are dynamic. So you simply need to test if the route matches a file that exists in the dist
directory or not, if not simply let .NET handle the route. Adding the following to your Web.config
file within the <system.webServer>
section should do the trick:
URL重写正是您想要的,使用条件块来测试文件是否存在。 dist目录下的内容是静态的(即存在于文件系统中),但WebApiApp路由是动态的。所以你只需要测试路由是否匹配dist目录中存在的文件,如果不是简单地让.NET处理路由。将以下内容添加到
<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>
The second rule is optional but it means a request for the root of your site will still serve the index.html
file from the dist
directory, effectively making the root of the project WebApiApp\dist
but still allowing all WebAPI routing.
第二个规则是可选的,但这意味着对站点根目录的请求仍然会从dist目录中提供index.html文件,从而有效地使项目WebApiApp \ dist的根目录仍然允许所有WebAPI路由。
#1
24
URL rewriting is exactly what you want, with a condition block to test if the file exists. The content under your dist
directory is static (i.e. lives on the file system) but the WebApiApp
routes are dynamic. So you simply need to test if the route matches a file that exists in the dist
directory or not, if not simply let .NET handle the route. Adding the following to your Web.config
file within the <system.webServer>
section should do the trick:
URL重写正是您想要的,使用条件块来测试文件是否存在。 dist目录下的内容是静态的(即存在于文件系统中),但WebApiApp路由是动态的。所以你只需要测试路由是否匹配dist目录中存在的文件,如果不是简单地让.NET处理路由。将以下内容添加到
<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>
The second rule is optional but it means a request for the root of your site will still serve the index.html
file from the dist
directory, effectively making the root of the project WebApiApp\dist
but still allowing all WebAPI routing.
第二个规则是可选的,但这意味着对站点根目录的请求仍然会从dist目录中提供index.html文件,从而有效地使项目WebApiApp \ dist的根目录仍然允许所有WebAPI路由。