using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using AttributeRoutingDemoInMVC.Models; namespace AttributeRoutingDemoInMVC.Controllers { [RoutePrefix("students")] public class StudentsController : Controller { private static List students = new List() { new Student() {Id = 1, Name = "Pranaya"}, new Student() {Id = 2, Name = "Priyanka"}, new Student() {Id = 3, Name = "Anurag"}, new Student() {Id = 4, Name = "Sambit"} }; [HttpGet] [Route] // This will be translated to /students public ActionResult GetAllStudents() { return View(students); } [HttpGet] [Route("{studentID:int:range(1,3)}")] // This will be translated to /students/2 public ActionResult GetStudentDetails(int studentID) { Student studentDetails = students.FirstOrDefault(s => s.Id == studentID); return View(studentDetails); } [HttpGet] [Route("{studentName:alpha}")] public ActionResult GetStudentDetails(string studentName) { Student studentDetails = students.FirstOrDefault(s => s.Name == studentName); return View(studentDetails); } [HttpGet] [Route("{studentID}/courses")] // This will be translated to /students/2/courses public ActionResult GetStudentCourses(int studentID) { List CourseList = new List(); if (studentID == 1) CourseList = new List() { "ASP.NET", "C#.NET", "SQL Server" }; else if (studentID == 2) CourseList = new List() { "ASP.NET MVC", "C#.NET", "ADO.NET" }; else if (studentID == 3) CourseList = new List() { "ASP.NET WEB API", "C#.NET", "Entity Framework" }; else CourseList = new List() { "Bootstrap", "jQuery", "AngularJs" }; ViewBag.CourseList = CourseList; return View(); } [Route("~/tech/teachers")] // This will be translated to /tech/teachers public ActionResult GetTeachers() { List teachers = new List() { new Teacher() {Id = 1, Name = "James"}, new Teacher() {Id = 2, Name = "Patrik"}, new Teacher() {Id = 3, Name = "Smith"} }; return View(teachers); } } }