Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 routing raising 404 errors

HTML5 PushState means refresh URL or enter direct URL in browser works perfectly fine when I run my angular app using 'npm start' command from VS 2015 command prompt. However when I deploy same application on IIS and change <base href="/"/> to <base href="/MyVirtualApplication/" /> then HTML5's PushState or History API doesn't work properly and raising 404 error.

Please do suggest.

like image 575
Avi Kenjale Avatar asked Jul 18 '26 14:07

Avi Kenjale


1 Answers

EDIT: IIS Setup for Url Rewriting:

You need to install this to make the following work: http://www.iis.net/downloads/microsoft/url-rewrite

First Step:

Second Step:

I found a link which explains the solution: https://stackoverflow.com/posts/12614072/

OLD:

While running any application built with Angular 2 outside of the development environment setup by npm, you have to replace <base href="/"> with this:

<script>document.write('<base href="' + document.location + '" />');</script>

Note: This is only for production or if running out of the npm environment.

like image 181
Bryan Avatar answered Jul 21 '26 09:07

Bryan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!