Introduction
In this article, we are going to implement a dynamic menu with the help of ASP.NET Web API and Angular JS.
We will create a login page that has two login types - Admin and User.
Admin login will lead to some different menu items and User login will come up with different menu items.
Prerequisite
- Programming of C#.
- Knowledge of JavaScript or AngularJS.
Implementation
Open Visual Studio.


Select ASP.NET Web Application, give the name of the solution, and click OK.
Create a class file, Menu. cs, inside the Models folder.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CustomMenuDemo.Models
{
public class Menu
{
List<Menu> MenuList;
public int MenuId { get; set; }
public string MenuName { get; set; }
public string MenuHref { get; set; }
public List<Menu> GetMenu(int id)
{
MenuList = new List<Menu>();
if (id == 1)
{
MenuList.Add(new Menu() { MenuId = 1, MenuName = "Add Employee", MenuHref="#" });
MenuList.Add(new Menu() { MenuId = 2, MenuName = "View Employee", MenuHref="#" });
MenuList.Add(new Menu() { MenuId = 3, MenuName = "Delete Employee", MenuHref="#" });
MenuList.Add(new Menu() { MenuId = 4, MenuName = "Edit Employee", MenuHref="#" });
MenuList.Add(new Menu() { MenuId = 5, MenuName = "Logout", MenuHref="#" });
}
else
{
MenuList.Add(new Menu() { MenuId = 1, MenuName = "Edit Details", MenuHref="#" });
MenuList.Add(new Menu() { MenuId = 2, MenuName = "My Task", MenuHref="#" });
MenuList.Add(new Menu() { MenuId = 3, MenuName = "Contact Us", MenuHref="#" });
MenuList.Add(new Menu() { MenuId = 4, MenuName = "Logout", MenuHref="#" });
}
return MenuList;
}
}
}
Add a Controller file inside the Controllers folder.

Give it a name as MenuController.

Add the Controller class. Modify the content as below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using CustomMenuDemo.Models;
namespace CustomMenuDemo.Controllers
{
public class MenuController : ApiController
{
// GET api/Menu
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/Menu/5
public List<Menu> Get(int id)
{
return new Menu().GetMenu(id);
}
// POST api/Menu
public void Post([FromBody]string value)
{
}
// PUT api/Menu/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/Menu/5
public void Delete(int id)
{
}
}
}
Create an App/js folder inside the Content directory.
Add index.js file with the following content.
var myapp = angular.module('uirouteApp', ["ui.router"]);
myapp.config(function ($stateProvider) {
$stateProvider
.state('index', {
url: "",
views: {
}
})
.state('Home', {
url: "/Home",
views: {
"secondView": {
template: "<span class='category-note'>Logged in Successfully!</span>"
}
}
});
});
myapp.controller('LoginController', function ($scope, $rootScope, $stateParams, $state, $http, LoginService, MenuService, UserService) {
$rootScope.title = "AngularJS Login Sample";
$scope.menus = [{ "MenuId": 1, "MenuName": "Home", "MenuHref": "#" }, { "MenuId": 2, "MenuName": "Contact Us", "MenuHref": "#" }];
$scope.username = '';
$scope.password = '';
$scope.usertype = '';
$scope.formSubmit = function (username, password, usertype) {
$scope.username = username;
$scope.password = password;
$scope.usertype = usertype;
if (LoginService.login($scope.username, $scope.password, $scope.usertype)) {
$http({
method: "GET",
url: "http://localhost:54824/api/Menu/" + $scope.usertype
}).then(function mySuccess(response) {
$scope.menus = response.data;
UserService.setUser({
username: $scope.username,
password: $scope.password,
usertype: $scope.usertype
});
$scope.$apply();
$state.go("Home");
}, function myError(response) {
$scope.message = response.statusText;
});
$scope.error = '';
$scope.username = '';
$scope.password = '';
} else {
$scope.error = "Incorrect username/password !";
}
};
});
myapp.factory('LoginService', function ($http) {
var isAuthenticated = false;
var menus = null;
return {
login: function (username, password, type) {
// fields validation check here
return true;
},
isAuthenticated: function () {
return isAuthenticated;
}
};
});
myapp.service('MenuService', function () {
var menus = {};
this.save = function (m) {
this.menus = m;
};
this.getMenus = function () {
return this.menus;
};
});
myapp.service('UserService', function () {
var user = {
username: "",
password: "",
usertype: ""
};
this.setUser = function (u) {
this.user = u;
};
this.getUser = function () {
return this.user;
};
});







Hamid KhanPosted Jan 28, 2018, 9:22 AM
Nice article, Irshad........................
Mehul PrajapatiPosted Jan 27, 2018, 11:31 AM
Simple and good example for beginner..
Saravanakumar SekaranPosted Jan 27, 2018, 2:23 AM
Wow simply awesomeeeeeee
Sagar Pandurang KapPosted Jan 24, 2018, 11:46 PM
Very nice.Keep posting...