-
settings to make angular(frontend) and asp.net MVC(backend) work from same URL.files generated by angular should be add at root location.
- Add rewrite rules in web.config(under
<system.webServer>) to allow correct routing of angular project on page refresh(F5).Make sure to install URL rewrite
<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" /> <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" /> </conditions> <action type="Rewrite" url="/" /> </rule> </rules> </rewrite>
- set in RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes){ routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); }
- Set default document/page to web.config(under
<system.webServer>).which will beindex.htmlbuild by angular.
<defaultDocument> <files> <clear /> <add value="index.html" /> </files> </defaultDocument>
- set in WebApiConfig.cs
public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API configuration and services // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); } }
- Add rewrite rules in web.config(under
- Add custom error settings to web.config(under
<configuration>) to return errors.
<system.web>
<customErrors mode="Off"/>
<compilation targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>- To facilitate CORS for allowing api to be called from diff domain.
- Add following to web.config(under
<appSettings>).
<add key="Production" value="false" /> <add key="URL_Production" value="http://prodURL:5015" /> <add key="URL_Testing" value="http://localhost:4200" />
- Add following to Global.asax.
protected void Application_BeginRequest(object sender, EventArgs e) { bool production = false; production = bool.Parse(ConfigurationManager.AppSettings["Production"].ToString()); string OrginURL_prod = ConfigurationManager.AppSettings["URL_Production"].ToString(); string OrginURL_testing = ConfigurationManager.AppSettings["URL_Testing"].ToString(); if (Context.Request.Path.Contains("api/") && Context.Request.HttpMethod == "OPTIONS") { if (production) { Context.Response.AppendHeader("Access-Control-Allow-Origin", OrginURL_prod); } else { Context.Response.AppendHeader("Access-Control-Allow-Origin", OrginURL_testing); } Context.Response.AppendHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); Context.Response.AppendHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); Context.Response.AppendHeader("Access-Control-Allow-Credentials", "true"); Context.Response.End(); } else if (Context.Request.Path.Contains("api/") && Context.Request.HttpMethod == "POST") { if (production) { Context.Response.AppendHeader("Access-Control-Allow-Origin", OrginURL_prod); } else { Context.Response.AppendHeader("Access-Control-Allow-Origin", OrginURL_testing); } Context.Response.AppendHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); Context.Response.AppendHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); Context.Response.AppendHeader("Access-Control-Allow-Credentials", "true"); } }
- Add following to web.config(under
- Set default document/page to web.config(under
<system.webServer>).
<defaultDocument>
<files>
<clear />
<add value="index.html" />
</files>
</defaultDocument>- Allow server to serve .woff .add following to web.config(under
<system.webServer>).solves issue when fonts are not loaded for UI libs like ui-grid.
<staticContent>
<clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
<remove fileExtension=".woff" />
<remove fileExtension=".woff2" />
<mimeMap fileExtension=".woff" mimeType="application/x-font-woff" />
<mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
</staticContent>