Introduction
In this article I am going to explain how does MVC Routing and Angular Routing work when used together.
If you have worked on MVC and Angular both but not together then you must have wondered that what will happen when both are used together. As both are having controllers, both are having routings and many more things.
It has become a hot topic for interviewers as well, as they try to mix things between Angular and MVC, just for e.g. they might ask what is the difference between AngularJS Controller and MVC Controller or between MVC routing and Angular Routing, or they might ask how you can use both the routings together and which one will work first. (OOPS! I am discussing some hot questions.)
I am not going to answer all these questions as this article is not about interview questions, here I'll only answer the last question i.e. How to use them together.
To explain this I created a sample application using MVC and AngularJS.
Step 1:
Firstly, an empty MVC application was created, in the application I add a controller file to the controller folder and just created a simple ActionResult which is calling the view Index.
- namespace MVCWithAngularJS.Controllers
- {
- public class HomeController : Controller
- {
- //
- // GET: /Home/
- public ActionResult Index()
- {
- return View();
- }
- }
- }
Step 2:
Right click on Index() and add click on Add View, this will create a folder inside Views folder with same name as of controller and in that folder a cshtml file will be created with name Index.

Step 3:
After this I worked on the cshtml page, here I called the Angular library, provided ng-app and ng-controller, and at last created a div where ng-view is applied, ng-view will make this div a container where we can call some other pages.
- <!DOCTYPE html>
- <html ng-app="angualarModule">
- <head>
- <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.min.js"></script>
- <script src="~/Scripts/AngularController.js"></script>
- </head>
- <body>
- <form ng-controller="indexController">
- <h2>I am Parent Page</h2>
- <fieldset>
- <legend>Getting data from controller</legend>
- <input type="text" ng-model="name" />
- </fieldset>
- <a href="/#/firstPage">Call First Page</a>
- <br />
- <a href="/#/secondPage">Call Second Page</a>
- <div ng-view></div>
- </form>
- </body>
- </html>
But prior to the ng-view div you can see that I had created hyperlinks on which some path is defined which is having "#(hash)" with them, this # is playing the major role in this article, you can say that # is the HERO here, but why so! You will get the answer at the end of article.
Step 4:
We have created a container which will hold some pages but which pages! For that I again went back to Controller folder and added a new controller named as "RouteDemoController".
In this controller I created two ActionResult "First and Second".
- namespace MVCWithAngularJS.Controllers
- {
- public class RouteDemoController : Controller
- {
- //
- // GET: /RouteDemo/
- public ActionResult First()
- {
- return View();
- }
- public ActionResult Second()
- {
- return View();
- }
- }
- }







Krushna GadePosted Feb 2, 2018, 12:09 AM
Great article ... thanks for sharing
shafeeq shafeeqPosted Jul 16, 2017, 7:35 AM
This not working with Asp.net core
Krishna NigalyePosted Apr 26, 2017, 10:32 AM
Thank you Anubhav Chaudhary for the article.
Rahul JainPosted Apr 19, 2017, 7:55 AM
Why you have not used _layout.cshtml file to place your menus link >? Is it a matter of choice or something different ?
Jimit BhattPosted Feb 1, 2017, 4:43 AM
This is really a nice article but I would like to inform those programmers who are facing issue in Step:5 in routing. If you are getting '#/! ' kind of characters in URL and not able to perform routing properly, then just use $locationProvider.hasprefix('') as shown: appmodule.config(function ($routeProvider, $locationProvider) { //locationProvider-> to clear hasprefix by default provided by angularjs $locationProvider.hashPrefix(''); $routeProvider.when('/Home', {.....................
Murali MPosted Dec 28, 2016, 11:38 AM
What if you set $locationProvider.html5Mode(true) in route config and directly open the url 'localhost/routedemo/first'.? MVC route/Angular route will win?
Kant NikhilPosted Dec 23, 2016, 1:04 PM
Very well explained , thanks for sharing this .
dinesh reddyPosted Dec 14, 2016, 6:34 AM
Finally..i got the solution..only angularJs 1.2.20 version works..previously i've used 1.6.0
dinesh reddyPosted Dec 14, 2016, 5:37 AM
But it didn't work. i am getting page url like this "http://localhost:1844/Home/Index#!#/firstPage". can you help me to sort out this..i am using VS 2013, MVC 4
Dmk GloriPosted Nov 15, 2016, 1:41 AM
Does it work with "Layout.cshtml" page?
Bharath PeggemPosted Aug 26, 2016, 3:49 AM
Hello, In case RouteConfig.cs (MVC) and ngRoute (Angjs) both same project, so it will works or not
chirag patelPosted Jun 15, 2016, 7:32 AM
Hello, is it possible to remove # from url.
Santhakumar MunuswamyPosted Jan 9, 2016, 2:27 AM
Thanks for nice article.
Anubhav ChaudharyPosted Jan 6, 2016, 11:15 PM
Thanks Al
Manoj KulkarniPosted Jan 6, 2016, 10:16 PM
Nice article. Thank you for sharing
Manas MohapatraPosted Jan 6, 2016, 1:14 PM
Nice to know Angular JS routing with MVC routing :)
Debasis SahaPosted Jan 6, 2016, 8:28 AM
Nice article..
Anubhav ChaudharyPosted Jan 6, 2016, 6:13 AM
Thank Abhishek Arora and Shridhar Sharma
Shridhar SharmaPosted Jan 6, 2016, 5:22 AM
nice share
Abhishek AroraPosted Jan 6, 2016, 4:41 AM
Good article