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