我要如何改变一个ASP.Net的WebAPI应用程序的根路径? [英] How do I change the root path of a ASP.Net WebAPI application?

查看:210
本文介绍了我要如何改变一个ASP.Net的WebAPI应用程序的根路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个页面的Web应用程序结合两种ASP.NET的WebAPI和自耕农Angluarjs发生器。目前,我有如下

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

| - yeomanAngularApp
    | - 应用程序
    | - DIST
        | - 脚本
        | - index.html的
    | - 等等......
| - WebApiApp
    | - App_Start
    | - 斌
    | - 内容
    | - 控制器
    | - 模型
    | - WebApiApp.csproj
    | - DIST
        | - 脚本
        | - index.html的

当我想建立的应用程序分配,我复制 DIST 文件夹中的 yeomanAngularApp WebApiApp 替换 DIST 文件夹在那里。

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.

现在这一切都是很容易做到的。我真的那么想要做的就是告诉 WebApiApp 不使用 WebApiApp \\ 作为项目的根,但使用 WebApiApp \\ DIST 。这意味着,而不是去 HTTP://localhost/dist/index.html ,我可以去 HTTP://localhost/index.html 即使 index.html的 DIST 文件夹中。在此之上我也想我的WebAPI路由的控制器很好地发挥了。

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.

我一直在寻找了一会儿,我似乎无法找到答案。最好我能想出,使用URL重写,这对我感觉不对。

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重写正是你想要的,一个条件块,以测试该文件是否存在。在你的 DIST含量目录是静态的(即住在文件系统上),但 WebApiApp 路线是动态的。所以,你只需要测试,如果路由存在于 DIST 目录或不是一个文件匹配,如果单纯让.NET处理路线。 system.webServer>的&LT中添加以下到你的的Web.config 文件部分应该做的伎俩:

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:

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

第二个规则是可选的,但它意味着你的网站的根仍将服务于的请求的index.html DIST文件目录,有效地使项目 WebApiApp \\ DIST 的根,但仍允许所有的WebAPI的路由。

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.

这篇关于我要如何改变一个ASP.Net的WebAPI应用程序的根路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆