{"id":11665,"date":"2017-06-14T14:00:00","date_gmt":"2017-06-14T14:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/premier_developer\/?p=11665"},"modified":"2019-02-20T09:57:09","modified_gmt":"2019-02-20T16:57:09","slug":"tips-for-running-an-angular-app-in-iis","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/premier-developer\/tips-for-running-an-angular-app-in-iis\/","title":{"rendered":"Tips for Running an Angular app in IIS"},"content":{"rendered":"<p><strong><a href=\"https:\/\/www.linkedin.com\/in\/atkinsonlaurie\/\" target=\"_blank\" rel=\"noopener\">Laurie Atkinson<\/a>, Premier Developer Senior Consultant,<\/strong> A few tweaks are necessary to take an Angular app and move it to IIS. Here is a list of changes required to get everything running smoothly.<\/p>\n<hr \/>\n<p>Using the Angular-CLI to generate an Angular SPA is an excellent way to scaffold out a well-organized application. This tool also includes a production build step that should allow you to <em>xcopy<\/em> your application to the web server of your choice. \u00a0We\u2019ll start here and discuss the necessary modifications to successfully run your application in IIS.<\/p>\n<h2>Use the Angular-CLI to create a production package<\/h2>\n<p>First decide whether the application will be located at the root in IIS (e.g. <a href=\"http:\/\/server-name\">http:\/\/server-name<\/a>) or in a path below the root (e.g. <a href=\"http:\/\/server-name\/my-app\">http:\/\/server-name\/my-app<\/a>).<\/p>\n<p>If deploying to the root, run the Angular CLI <em>build<\/em> command:<\/p>\n<p><img decoding=\"async\" width=\"235\" height=\"37\" class=\"wp-image-35208\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/06\/cidimage003-jpg01d2d304-8dbdecd0.jpeg\" alt=\"cid:image003.jpg@01D2D304.8DBDECD0\" \/><\/p>\n<p>If deploying to a path beneath the root, run the Angular CLI <em>build<\/em> command with the <em>base-href<\/em> option set to the name of application directory.<\/p>\n<p><img decoding=\"async\" width=\"486\" height=\"30\" class=\"wp-image-35209\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/06\/cidimage004-jpg01d2d304-8dbdecd0.jpeg\" alt=\"cid:image004.jpg@01D2D304.8DBDECD0\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/06\/cidimage004-jpg01d2d304-8dbdecd0.jpeg 486w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/06\/cidimage004-jpg01d2d304-8dbdecd0-300x19.jpeg 300w\" sizes=\"(max-width: 486px) 100vw, 486px\" \/><\/p>\n<p>The <em>base-href<\/em> option modifies the base element in the <em>index.html<\/em>, so it looks as follows. This is required for routing to work correctly.<\/p>\n<p><img decoding=\"async\" width=\"273\" height=\"114\" class=\"wp-image-35210\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/06\/cidimage005-jpg01d2d304-8dbdecd0.jpeg\" alt=\"cid:image005.jpg@01D2D304.8DBDECD0\" \/><\/p>\n<h2>Create an IIS web application<\/h2>\n<p>Add the web site, or application, or virtual directory to IIS and set the physical path to the location of the <em>dist<\/em> folder created by the Angular CLI.<\/p>\n<p><img decoding=\"async\" width=\"442\" height=\"377\" class=\"wp-image-35211\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/06\/cidimage006-jpg01d2d304-8dbdecd0.jpeg\" alt=\"cid:image006.jpg@01D2D304.8DBDECD0\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/06\/cidimage006-jpg01d2d304-8dbdecd0.jpeg 442w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/06\/cidimage006-jpg01d2d304-8dbdecd0-300x256.jpeg 300w\" sizes=\"(max-width: 442px) 100vw, 442px\" \/><\/p>\n<p>Any one of these three options shown below will work.<\/p>\n<p><img decoding=\"async\" width=\"330\" height=\"209\" class=\"wp-image-35212\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/06\/cidimage007-jpg01d2d304-8dbdecd0.jpeg\" alt=\"cid:image007.jpg@01D2D304.8DBDECD0\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/06\/cidimage007-jpg01d2d304-8dbdecd0.jpeg 330w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/06\/cidimage007-jpg01d2d304-8dbdecd0-300x190.jpeg 300w\" sizes=\"(max-width: 330px) 100vw, 330px\" \/><\/p>\n<h2>Install the URL Rewrite Module into IIS<\/h2>\n<p>This next step is required to support deep-linking, which is the capability for the user to navigate directly to a page by typing the route into the address bar instead of using the Angular routing. Deep-linking causes a problem for IIS because the URL that the user attempts to access is not known to the server and therefore the user receives a 404 response. The solution is for the server to always return the root of the application, even if the user requests a path within the application.<\/p>\n<p>Get the URL Rewrite Module installer here: <a href=\"https:\/\/www.iis.net\/downloads\/microsoft\/url-rewrite\">https:\/\/www.iis.net\/downloads\/microsoft\/url-rewrite<\/a><\/p>\n<p>After installing you should see a new icon in the IIS Manager.<\/p>\n<p><img decoding=\"async\" width=\"507\" height=\"152\" class=\"wp-image-35213\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/06\/cidimage008-jpg01d2d304-8dbdecd0.jpeg\" alt=\"cid:image008.jpg@01D2D304.8DBDECD0\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/06\/cidimage008-jpg01d2d304-8dbdecd0.jpeg 507w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/06\/cidimage008-jpg01d2d304-8dbdecd0-300x90.jpeg 300w\" sizes=\"(max-width: 507px) 100vw, 507px\" \/><\/p>\n<h2>Add web.config file with a URL Rewrite rule<\/h2>\n<p>All requests to this web application that are not for files or folders should be directed to the root of the application. For an application or virtual directory under the default web site, the URL should be set to the alias, (e.g. \u201c\/MyApp\/\u201d). For a web site at the root of the server, the URL should be set to \u201c\/\u201d.<\/p>\n<p><code>&lt;configuration&gt;<\/code><\/p>\n<p><code>&lt;system.webServer&gt;<\/code><\/p>\n<p><code>\u00a0 &lt;rewrite&gt;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0 &lt;rules&gt;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;rule name=\"Angular Routes\" stopProcessing=\"true\"&gt;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;match url=\".*\" \/&gt;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;conditions logicalGrouping=\"MatchAll\"&gt;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;add input=\"{REQUEST_FILENAME}\" matchType=\"IsFile\" negate=\"true\" \/&gt;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;add input=\"{REQUEST_FILENAME}\" matchType=\"IsDirectory\" negate=\"true\" \/&gt;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/conditions&gt;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;action type=\"Rewrite\" url=\"\/MyApp\/\" \/&gt;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;!--&lt;action type=\"Rewrite\" url=\"\/\" \/&gt;--&gt;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/rule&gt;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0 &lt;\/rules&gt;<\/code><\/p>\n<p><code>\u00a0 &lt;\/rewrite&gt;<\/code><\/p>\n<p><code>&lt;\/system.webServer&gt;<\/code><\/p>\n<p><code>&lt;\/configuration&gt;<\/code><\/p>\n<h2>Troubleshooting<\/h2>\n<p><strong><em>Problem:<\/em><\/strong> Navigating directly to a route results in a 404.<\/p>\n<p><strong><em>Solution:<\/em><\/strong> Make sure the <em>web.config<\/em> is correct and the URL property in the <em>web.config<\/em> matches both the <em>base href<\/em> in the <em>index.html<\/em> and the web application or virtual directory alias if necessary.<\/p>\n<p><strong><em>Problem:<\/em><\/strong> Navigating directly to a route results in a 500 or an error message related to the <em>web.config<\/em>.<\/p>\n<p><strong><em>Solution:<\/em><\/strong> Install the URL Rewrite module.<\/p>\n<p><strong><em>Problem:<\/em><\/strong> Navigating directly to a route displays a broken page and network\/console errors show 404 results for <em>js<\/em> and <em>css<\/em> requests.<\/p>\n<p><strong><em>Solution:<\/em><\/strong> Verify that the <em>base href<\/em> in the <em>index.html<\/em> exactly matches the URL in the rewrite rule and that the slashes surrounding the name are not missing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Laurie Atkinson, Premier Developer Senior Consultant, A few tweaks are necessary to take an Angular app and move it to IIS. Here is a list of changes required to get everything running smoothly. Using the Angular-CLI to generate an Angular SPA is an excellent way to scaffold out a well-organized application. This tool also includes [&hellip;]<\/p>\n","protected":false},"author":582,"featured_media":37840,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[122],"tags":[51,242,109,52],"class_list":["post-11665","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular","tag-angular","tag-iis","tag-javascript","tag-laurie-atkinson"],"acf":[],"blog_post_summary":"<p>Laurie Atkinson, Premier Developer Senior Consultant, A few tweaks are necessary to take an Angular app and move it to IIS. Here is a list of changes required to get everything running smoothly. Using the Angular-CLI to generate an Angular SPA is an excellent way to scaffold out a well-organized application. This tool also includes [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/11665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/users\/582"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/comments?post=11665"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/11665\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media\/37840"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media?parent=11665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/categories?post=11665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/tags?post=11665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}