title: AngularJS with ASP.net MVC
body: <p>Okay, this seems to be getting the better of me.</p>
  
  <p>I've been reading blog after blog, where guys explain how to get this going, but after countless hours of removing, adding, changing, debugging, pulling-at-hair, copious amounts of coffee and no luck in getting this to work, I'm turning here for some professional insight.</p>
  
  <p>As the title suggest, I'm trying to get angular to work within an MVC 5 with Razor views, but I'm experiencing some very weird behavior.</p>
  
  <p>Firstly, angular does not kick in on page load. I have to refresh the landing page at least 5 - 10 times before it kicks in.</p>
  
  <p>When it eventually works, Chrome goes into overburn, consuming nearly all of the available resources before crashing. Console in developer tools comes up with a message "Warning: Trying to load angular multiple times".</p>
  
  <p>The error is very descriptive, so I assume that I'm loading the _layout.cshtml page twice, when the client-side routing eventually kicks in, but each Razor view contains the @{ Layout = null } call, as it has been explained on so many blogs.</p>
  
  <p>I've tried everything I could possibly read up on, and I cannot get passed this issue.</p>
  
  <p>Following, my code:</p>
  
  <p><strong>ASP.Net Razor View</strong></p>
  
  <p><strong><em>1.) _Layout.cshtml</em></strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" data-ng-app="webStudio" data-ng-controller="StudioController"&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8" /&gt;
      &lt;meta name="viewport" content="width=device-width" /&gt;
      &lt;title data-ng-bind="Page.title"&gt;...&lt;/title&gt;
      &lt;base href="/" /&gt;
      @Styles.Render("~/Content/css")
      @Scripts.Render("~/bundles/modernizr")
  &lt;/head&gt;
  &lt;body&gt;
  
      &lt;div class="page {{css}}" data-ng-view=""&gt;
          @RenderBody()
      &lt;/div&gt;
  
      &lt;div class="__javascript-webstudio"&gt;
          @Scripts.Render("~/bundles/jquery")
          @Scripts.Render("~/bundles/ng")
          @Scripts.Render("~/bundles/yeti")
          @Scripts.Render("~/bundles/ngapp")
          @Scripts.Render("~/bundles/ngfactory")
          @Scripts.Render("~/bundles/ngservice")
          @Scripts.Render("~/bundles/ngcontroller")
          @RenderSection("scripts", required: false)
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I assumed that, because the <code>_layout.cshtml</code> is used for the index page (Home), it is where the angular instantiate should occur. I've seen blogs where they skip the <code>_layout.cshtml</code> all together, but to keep it consistent, I want this to be my starting point. I have tried to not use Layout in Home/Index, and call all my scripts in there, but it does the exact same thing, couple of refreshes, and Warning.</p>
  
  <p><strong><em>2.) Home/Index.cshtml</em></strong></p>
  
  <pre><code>@*{
      Layout = null
  }*@
  
  &lt;a href="@Url.Action("Index", "Gallery")"&gt;Go there&lt;/a&gt;
  </code></pre>
  
  <p><code>Layout = null</code> has been commented out, because this causes <code>_layout.cshtml</code> to not even load my JS files, weird behaviour :/</p>
  
  <p><strong><em>3.) Gallery/Index.cshtml</em></strong></p>
  
  <pre><code>@{
      Layout = null;
  }
  
  Date: @DateTime.Now
  </code></pre>
  
  <p>This is just a page to test the route.</p>
  
  <p><strong><em>4.) Web.config</em></strong></p>
  
  <pre><code>&lt;rewrite&gt;
      &lt;rules&gt;
          &lt;rule name="AngularJS" stopProcessing="true"&gt;
              &lt;match url=".*" /&gt;
              &lt;conditions logicalGrouping="MatchAll"&gt;
                  &lt;add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /&gt;
                  &lt;add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /&gt;
                  &lt;add input="{REQUEST_URI}" pattern="^/(api)" negate="true" /&gt;
              &lt;/conditions&gt;
              &lt;action type="Rewrite" url="/" /&gt;
          &lt;/rule&gt;
      &lt;/rules&gt;
  &lt;/rewrite&gt;
  </code></pre>
  
  <p>The rewrite rule is important to ensure that the routing is done appropriately. I am using HTMLMode for angular, so this step is vital.</p>
  
  <p><strong><em>5.) app.js</em></strong></p>
  
  <pre><code>var webStudio = angular.module("webStudio", ["ngRoute"]);
  
  webStudio.config(function ($routeProvider, $locationProvider)
  {
      $routeProvider
          .when("", {
              templateUrl: "/Home",
              controller: "HomeController"
          })
          .when("/", {
              templateUrl: "/Home",
              controller: "HomeController"
          })
          .when("/Home", {
              templateUrl: "/Home",
              controller: "HomeController"
          })
          .otherwise({ redirectTo: "/Error/NotFound" });
  
      $locationProvider.html5Mode(true);
  });
  </code></pre>
  
  <p>There really is nothing weird about this, it creates the angular app reference, attaches the routing to the module and form there I configure the routing behavior.</p>
  
  <p>From what I've read and understand, it makes sense to call the controller/action block as you would do normally in ASP.Net MVC.</p>
  
  <p>The default routing for MVC I did not touch, as it's irrelevant in this scope.</p>
  
  <p>I'm at a loss as to why this behavior is occurring and don't know where to start investigating. I've read blogs on the refresh issue and have tried what has been stated, but in this scenario, I just cannot get it to work.</p>
  
  <p>Update</p>
  
  <p><strong><em>6.) BundleConfig.cs</em></strong></p>
  
  <pre><code>public static void RegisterBundles(BundleCollection bundles)
  {
      bundles.Add(new ScriptBundle("~/bundles/jquery")
          .Include("~/Scripts/vendor/jquery/jquery-{version}.js")
          .Include("~/Scripts/vendor/jquery/jquery.validate.js"));
  
      bundles.Add(new ScriptBundle("~/bundles/ng")
          .Include("~/Scripts/vendor/angularjs/angular.js")
          .Include("~/Scripts/vendor/angularjs/angular-route.js"));
  
      bundles.Add(new ScriptBundle("~/bundles/yeti")
          .Include("~/Scripts/vendor/ngfoundation/mm-foundation-0.6.0.js"));
  
      bundles.Add(new ScriptBundle("~/bundles/ngapp")
          .Include("~/Scripts/app.js"));
  
      bundles.Add(new ScriptBundle("~/bundles/ngfactory")
          .Include("~/Scripts/factories/studio.js"));
  
      bundles.Add(new ScriptBundle("~/bundles/ngservice")
          .Include("~/Scripts/services/studio.js"));
  
      bundles.Add(new ScriptBundle("~/bundles/ngcontroller")
          .Include("~/Scripts/controllers/studio.js"));
  
      bundles.Add(new ScriptBundle("~/bundles/modernizr")
          .Include("~/Scripts/vendor/modernizr-*"));
  
      bundles.Add(new StyleBundle("~/Content/css")
          .Include("~/Content/normalize.css")
          .Include("~/Content/foundation.css")
          .Include("~/Content/site.css"));
  }
  </code></pre>