Search code examples
asp.netangulariisweb-config

Angular Routing in IIS server


I have an Angular 4 app in a IIS server, in the same server there is a .NET Web API. They are in diferents folders: angular app is in "/wwwroot/angular/" and web api in "/wwwroot/api/". When I do a request to web api, it works successfully, but when I try to navigate to an URL different to index.html using the Routing Module in angular app I get this message:

500 - Internal server error. There is a problem with the resource you are looking for, and it cannot be displayed.

Also, I have two Web.Config files -one in each folder-.

My Angular Web. Config is:

<system.webServer>
 <rewrite>
  <rules>
   <rule name="Angular Routes" stopProcessing="true">
     <match url=".*" />
     <conditions logicalGrouping="MatchAll">
       <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
       <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
     </conditions>
     <action type="Rewrite" url="/index.html" />
   </rule>
  </rules>
 </rewrite>
</system.webServer>

Web.config of WEB API

<configuration>
 <system.webServer>
  <handlers>
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <remove name="OPTIONSVerbHandler" />
    <remove name="TRACEVerbHandler" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  </handlers>
 </system.webServer>
</configuration>

I researched in some questions like:

stackoverflow.com/questions/49833141/blank-page-when-publishing-angular-5-net-core-web-api

and

stackoverflow.com/questions/42865084/redirect-unknown-requests-to-index-html-in-springboot

But they doesn't work to me.

Anybody help me on this.

Thanks in advance.


Solution

  • The solution was move all the Angular files to root, at index.html I left <base href="/"> and make the web.config like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
     <system.webServer>
        <rewrite>
            <rewriteMaps>
                <rewriteMap name="^(.*)$" />
            </rewriteMaps>
            <rules>
                <rule name="Angular Route" stopProcessing="true">
                    <match url="^(.*)$" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_URI}" pattern="/api(.*)$" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/index.html" />
                </rule>
            </rules>
        </rewrite>
        <security>
            <authorization>
                <remove users="*" roles="" verbs="" />
                <add accessType="Allow" users="?" />
            </authorization>
        </security>
     </system.webServer>
    </configuration>