I have simple app with one component that expects certain parameters from url. there is only one route in the application:
const appRoutes: Routes = path: 'hero/:userId/:languageId',component: HeroWidgetComponent }];
In the Index.html, I have this in the header <base href="/">
I am using webpack and the application runs fine on development environment, when browsing the url: http://localhost:4000/hero/1/1
.
However, when building the app for production and getting the distribution files, then hosting that on IIS. I get the following Error when trying to browse the same url:
HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
The application works fine if I remove all the routing and just browse: http:localhost:4200
on the IIS.
Resolution. To resolve this problem, verify that the file requested in the browser's URL exists on the IIS computer and that it is in the correct location. Use the IIS Microsoft Management Console (MMC) snap-in to determine where the file requested must exist in the IIS computer's file system.
To set up a 404 page in the angular routing, we have to first create a component to display whenever a 404 error occurred. In the following approach, we will create a simple angular component called PagenotfoundComponent. Creating Component: Run the below command to create pagenotfound component.
Enable IIS & Install URL Rewrite. Configure Angular to output files in the development build. Add a new application in IIS. IIS permissions.
A 404 error is an HTTP status code that means that the page you were trying to reach on a website couldn't be found on their server. To be clear, the error indicates that while the server itself is reachable, the specific page showing the error is not.
We have to make IIS fall back to index.html by adding a rewrite rule.
Step 1: Install IIS URL Rewrite Module
Step 2: Add a rewrite rule to web.config
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="AngularJS 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="/" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With