Skip to content

Latest commit

 

History

History
141 lines (128 loc) · 5.67 KB

File metadata and controls

141 lines (128 loc) · 5.67 KB

Asp.net

Angular project setup

  • settings to make angular(frontend) and asp.net MVC(backend) work from same URL.files generated by angular should be add at root location.

    1. 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>       
    1. set in RouteConfig.cs
    public static void RegisterRoutes(RouteCollection routes){
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    }
    1. Set default document/page to web.config(under <system.webServer>).which will be index.html build by angular.
        <defaultDocument>
            <files>
                <clear />
                <add value="index.html" />                
            </files>
        </defaultDocument>
    1. 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 }
            );
        }
    }

Custom error

  • 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>

CORS workaround

  • To facilitate CORS for allowing api to be called from diff domain.
    1. 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" />    
    1. 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");
                }
            }

Default document page

  • Set default document/page to web.config(under <system.webServer>).
    <defaultDocument>
         <files>
             <clear />
             <add value="index.html" />                
         </files>
    </defaultDocument>

Serve woff files

  • 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>