Tuesday, 10 April 2018

MVC 5 Features

What’s New in ASP.NET MVC 5

Attribute Routing
This is where attribute based routing comes in. Using attribute based routing we can define the route in the same place where action method is defined.
     public ActionResult GetElectronicItems(string id)
         ViewBag.Id = id;
          return View();

To enable attribute based routing we need to add the following in the RouteConfig file.

      public static void RegisterRoutes(RouteCollection routes)

Optional Parameter
We can also specify if there is any optional parameter in the URL pattern defined by the Route attribute with the “?” character.

      public ActionResult GetElectronicItems(int? id) {
          ViewBag.Id = id; return View();


Route constraints
We can also specify parameter constraints placing the constraint name after the parameter name separated by colon. For example we can specify that the parameter type should be integer by using the following


Route Prefix
If we have multiple action methods in a controller all using the same prefix we can use RoutePrefix attribute on the controller instead of putting that prefix on every action method.

Like we can attach the following attribute on the controller

So now our Route attribute on our action method does not need to specify the common prefix

Filter's in MVC
Filters in MVC provide us with an elegant way to implement cross cutting concerns.Cross cutting concerns is the functionality that is used across our application in different layers.Common example of such functionality includes caching ,exception handling and logging.

We can create global or controller filters in MVC 4.Global filters are filters that are applied to all the action methods in the application while controller filters apply to all the action methods in the controller.

We can create a global filter by creating a class and registering it as a global filter

public class TestGlobalFilterAttribute : ActionFilterAttribute
       public override void OnActionExecuting(ActionExecutingContext context)
           context.RequestContext.HttpContext.Response.Write("Global filter's in MVC are cool...");

//Register our global filter
GlobalFilters.Filters.Add(new TestGlobalFilterAttribute()); 

Filter Overrides in MVC 5
Now if want to override our global action filter in one of our action method's then we just need to apply the OverrideActionFilters attribute.Below we have applied the attribute to the default About method in the HomeController.

public ActionResult About(){
ViewBag.Message = "Your application description page.";
return View();

Scaffolding means generating code automatically from the model.Suppose we have some model for which we want to generate the skeleton code for the basic Create Read Update and Delete operations then instead writing the common code we can use Scaffolding feature of MVC 5.

Twitter Bootstrap is added as the default user interface framework for an MVC application. Bootstrap is a free collection of HTML and CSS based design templates created at Twitter for designing forms, navigation, buttons, tables etc.

ASP.NET Identity



Post a Comment


ADFS (1) ADO .Net (2) Ajax (1) Angular (1) Angular Js (17) Angular2 (28) ASP .Net (14) Authentication (1) Azure (1) Breeze.js (1) C# (50) CD (1) CI (2) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DevOps (4) DI (4) Dotnet (22) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (8) Lamda (3) Linq (11) Mongodb (1) MVC (50) NodeJS (7) RDLC (1) Report (1) SDLC (1) Sql Server (30) SSIS (3) SSO (1) SSRS (2) UI (1) WCF (13) Web Api (11) Web Service (1) XMl (1)