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.
   
[Route("Products/Electronics/{id}")]
     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)
       {
           routes.MapMvcAttributeRoutes();
       }

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

      [Route("Products/Electronics/{id?}")]
      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("Products/Electronics/{id:int}")]

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
[RoutePrefix("Products")]

So now our Route attribute on our action method does not need to specify the common prefix
[Route("Electronics/{id}")]

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)
       {
           base.OnActionExecuting(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.

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

Scaffolding
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.

Bootstrap
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

One ASP.NET

0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives