Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何更改ASP.NETWebAPI应用程序的根路径?_Asp.net_Asp.net Web Api - Fatal编程技术网

如何更改ASP.NETWebAPI应用程序的根路径?

如何更改ASP.NETWebAPI应用程序的根路径?,asp.net,asp.net-web-api,Asp.net,Asp.net Web Api,我正在尝试创建一个结合ASP.NET WebAPI和Yeoman Angluarjs生成器的单页web应用程序。目前,我的项目结构如下所示 |--Yeomanangulapp |--应用程序 |--距离 |--剧本 |--index.html |--等等。。。 |--WebApiApp |--应用程序启动 |--垃圾箱 |--内容 |--控制器 |--模型 |--WebApiApp.csproj |--距离 |--剧本 |--index.html 当我想要构建应用程序分发时,我将yeomana

我正在尝试创建一个结合ASP.NET WebAPI和Yeoman Angluarjs生成器的单页web应用程序。目前,我的项目结构如下所示

|--Yeomanangulapp
|--应用程序
|--距离
|--剧本
|--index.html
|--等等。。。
|--WebApiApp
|--应用程序启动
|--垃圾箱
|--内容
|--控制器
|--模型
|--WebApiApp.csproj
|--距离
|--剧本
|--index.html

当我想要构建应用程序分发时,我将
yeomanangularp
中的
dist
文件夹复制到
WebApiApp
中,替换其中的
dist
文件夹

现在这一切都很容易做到。然后我真正想做的是告诉
WebApiApp
不要使用
WebApiApp\
作为项目的根,而是使用
WebApiApp\dist
。这意味着不去
http://localhost/dist/index.html
,我可以去
http://localhost/index.html
即使
index.html
位于
dist
文件夹中。除此之外,我还希望控制器的WebAPI路由也能很好地发挥作用


我已经搜索了一段时间,似乎找不到答案。我能想到的最好办法是使用URL重写,这对我来说是不对的。

URL重写正是您想要的,有一个条件块来测试文件是否存在。
dist
目录下的内容是静态的(即存在于文件系统中),但
WebApiApp
路由是动态的。因此,您只需测试路由是否与
dist
目录中存在的文件匹配,如果不是简单地让.NET处理路由的话。将以下内容添加到
部分中的
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路由。

取决于您如何为应用程序提供服务。您正在使用iis吗?@rik.vanmechelen yep。我正在使用IISWow Seb为应用程序提供服务,它工作得很好,这是解决问题的一个极好的解决方案!如果我想将我的url“/dist/admin/index.html”更改为“/admin”,我该怎么做?