AngularJs routing with Asp.Net Mvc

22,243

Solution 1

I think you have some misonceptions about Angularjs routing concepts.

MVC Routing :

ASP.NET routing enables you to use URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a file, you can use URLs that are descriptive of the user's action and therefore are more easily understood by users.

Angular Routing :

Angular.js routing using MVC framework does not use MVC routing.

Comparable parts are:

Model ===== ng-module 
controller ===== ng-controller
view ===== ng-view 

So you can't call the MVC Controller in your angularjs route config. Does this make sense?

Also please think about some of the differences between cshtml and html.

Angular routing and MVC routing are totally different because:

  • Angular routing is use client side
  • MVC routing is used server side

The above texts are for your understanding only.

I think this discussion will help you :

How to use ASP.NET MVC and AngularJS routing?

Update :

The href is wrong in Anchor tag.

Its should be href="#/Home", not href="#Home"

So please change your code

 <a href="#/Home">Home</a>
    <a href="#/Projects">Projects</a>

Solution 2

Lets understand the routing in angular first. lets say your url says

www.example.com/Home/Index -- this points to your MVC HomeController and Index ActionMethod. Now what mvc does, it returns you the first View only.

say you have an anchor Load the New View. Clicking this will result in a new Url www.example.com/Home/Index#/angular-route. This url will still hit the same MVC HomeController and ActionMethod. But you have an additional route in angular

`var app = angular.module('ProjectTrackingModule', ['ngRoute', 'ui.bootstrap']);
 app.config(function ($routeProvider) {
    $routeProvider
        .when("/angular-route", {
        templateUrl: "/Html/GetHtml?type=angular-route",
        controller:"AngularController"
    })   
    .otherwise({redirectTo:"/Home"})
});`

Here in the code section templateUrl: "/Html/GetHtml?type=angular-route",

Html is MVC Controller and GetHtml is ActionMethod. This ActionMethod returns you the new view that you want according to the angular route, that's why we are sending a parameter type too to help us decide.

controller:"AngularController" says that angular will call its controller after the page is returned from you mvc controller. It's Angular's Controller and it has nothing to do with your MVC controller.

you declare angular controller like this:

 app.controller('AngularController',function($scope){
        alert("my js controller is called");
    });

Hope this helps you to find a solution.

Solution 3

Have a simple example can apply to your project. Example our MVC application has four pages as Home, Contact, About and User, we want to create a angularjs template for each pages, so how we do routing for it? how to make angularjs controller for it?

Basic code as following:

Routing:

    $routeProvider
    .when('/', { // For Home Page
        templateUrl: '/AngularTemplates/Home.html',
        controller: 'HomeController'
     })
    .when('/Home/Contact', { // For Contact page
        templateUrl: '/AngularTemplates/Contact.html',
        controller: 'ContactController'
    })
    .when('/Home/About', { // For About page
        templateUrl: '/AngularTemplates/About.html',
        controller: 'AboutController'
    })
    .when('/Home/User/:userid', { // For User page with userid parameter
       templateUrl: '/AngularTemplates/User.html',
       controller: 'UserController'
    })
    .otherwise({   // This is when any route not matched => error
       controller: 'ErrorController'
    })
}]);

Controller:

var app = angular.module('MyApp'); app.controller('UserController', ['$scope', '$routeParams', function ($scope, $routeParams) {
$scope.Message = "This is User  Page with query string id value = " + $routeParams.userid;}]); ...

Full article with download code for it at Angularjs routing asp.net mvc example

Share:
22,243
Amit Kumar
Author by

Amit Kumar

There's only one religion, StackOverflow.

Updated on July 21, 2022

Comments

  • Amit Kumar
    Amit Kumar almost 2 years

    I'm trying to build a SPA with Asp.Net MVC. for this I'm using angularJs routing . This is my project hierarchy. enter image description here

    My Layout.cshtl code

    <html lang="en" ng-app="ProjectTrackingModule">
     <head>
       <script src="~/Scripts/jquery-2.1.0.min.js"></script>
       <script src="~/Scripts/angular.min.js"></script>
       <script src="~/Scripts/angular-route.min.js"></script>
       <script src="~/Scripts/app.js"></script>
    </head>
    <body>
    <a href="#Home">Home</a>
    <a href="#Projects">Projects</a>
       <div ng‐view style="margin‐left: 10%; margin‐right: 10%;">
        </div>
    //... footer
    </body>
    </html>
    

    My app.Js code is as follow:

    var app = angular.module('ProjectTrackingModule', ['ngRoute', 'ui.bootstrap']);
    app.config(function ($routeProvider) {
        $routeProvider
            .when("/Home", {
            templateUrl: "/Views/Home/Home.cshtml",
            controller:"HomeController"
        })
        .when("/Projects", {
            templateUrl: "/Views/ProjectManagement/ProjectDetails.cshtml",
        controller: "ProjectsController"
        })
        .otherwise({redirectTo:"/Home"})
    });
    

    I want to load my Home.Cshtml partial view inside ng-view. But when I run my application, It only showing Home partial view.

    also when I click on Project, then it should render ProjectDetails.cshtml inside ng-view.

    code inside ProjectDetails.cshtml

    <div ng-controller="ProjectsController">
        <h2>ProjectDetails</h2>
    </div>