【问题标题】:Change the root path ASP WebApi更改根路径 ASP WebApi
【发布时间】:2017-03-02 14:41:14
【问题描述】:

我的 Angular 项目位于 app 文件夹中,因此我必须将其发送至 http://localhost:45485/app,但我想从该 URL 中删除 app。我正在尝试这样做,但它不起作用。我不知道该怎么办,请帮帮我。

web.config 文件:

<rewrite>
  <rules>
    <clear/>
    <rule name="app" stopProcessing="true">
      <match url="^(.+)$" />
      <conditions>
        <add input="{APPL_PHYSICAL_PATH}app\{R:1}" matchType="IsFile" />
      </conditions>
      <action type="Rewrite" url="/app/{R:1}" />
    </rule>

    <rule name="index.html as document root" stopProcessing="true">
      <match url="^$" />
      <action type="Rewrite" url="/app/index.html" />
    </rule>
  </rules>
</rewrite>

结果我遇到了这个问题:

【问题讨论】:

  • 虽然我对如何(正确地)做到这一点很感兴趣,但对“为什么”感到好奇?对于“web api”应用程序?如果这是一个前端角项目(仅),那么它似乎是“方孔中的圆钉”....
  • @EdSF,我只是研究了一下,有些时候看不懂,为什么不呢?而且在我看来,这是真的,所以我想试试。

标签: asp.net .net asp.net-web-api


【解决方案1】:

我可能没有得到这个问题的“目的”或问题的要点,所以假设你只是希望你的 SPA 成为“主要”入口点(又名http://example.com/),虽然您的 Angular 资产都在 /app 文件夹中,但无需处理重写规则,也无需影响(整个)Asp.Net/MVC/Web Api 应用程序(因为重写规则)。

不是 Angular “专家”,所以这是一个供您改进的简单示例:

  • 您的ng-app 可以很好地放置在ASP.Net MVC/Web Api 应用程序的“默认”(“home”->“index”)View 中——如果您愿意,也可以是“容器”。作为您网站的“索引”(首页/默认页面),不需要/不需要“重写”规则。

    index.cshtml MVC View 使用ui-router 的简单示例

    <div id="foo" ng-app="myNgApp">
      <ui-view></ui-view>
    </div>
    
  • 您的 Angular 资产可以在 /app 文件夹中

    /app
      index.html
      page2.html
    
  • 使用 ui-router(见上面的链接),它会像这样

    $stateProvider
    .state('home', {
       templateUrl: '/app/index.html' //maps to root -> app -> index.html
     })
     .state('foo', {
       templateUrl: '/app/page2.html' //maps to root -> app -> page2.html
     })
    

请注意,我在templateUrl 中使用资源的完整路径(“root relative”),因为它映射到您的网站/应用程序文件夹,而不仅仅是相对(到app 子文件夹)。

Hth.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 2013-08-08
    • 2018-12-13
    • 2019-10-15
    • 2020-02-12
    • 1970-01-01
    相关资源
    最近更新 更多