How do I change the root path of a ASP.Net WebAPI application?

后端 未结 1 1239
无人及你
无人及你 2020-12-08 08:46

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

相关标签:
1条回答
  • 2020-12-08 09:22

    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>
    

    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.

    0 讨论(0)
提交回复
热议问题