Asp.Net MVC 5 – Scaffolding Enhancements

Scaffolding Enhancements

Scaffolding is a feature through which we can generate code automatically using some predefined templates. Asp.Net MVC has built in scaffolding support and feature has been enhanced in MVC 5 by introduction of New Scaffold Menu item.

Let’s try to generate a controller for an existing Model class

public class PersonModel
{
[Key]
public int PersonId { get; set; } 

public string PersonName { get; set; } 

public int PersonAge { get; set; }
}

Continue reading

Asp.Net MVC 5 – Authentication Filters

Authentication Filters

One of a very common question asked in interview is difference between Authentication and Authorization.

Authentication is Who all are allowed to enter site.

Authorization is What all they are allowed to access once they have entered the site.

Before MVC 5 we already had Authorization Filters which we use to control access to resources i.e. whether the current user principle has access to requested resource on server. Continue reading

Asp.Net MVC 5 – Override Filters

Override Fiters

Asp.net MVC 5 provides a way to control which filter applies to which controller or action methods. This can be achieved using Override Filters.

As you might be aware that Filters can be applied to controllers or Actions or Global level. We can apply filters at higher levels usually globally or at controller level and then depending on use we can override them at a lower level i.e. Controller and Action respectively.

Let’s apply Authorize attribute which is an Authorization Filter at Global Level.

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
 filters.Add(new AuthorizeAttribute(){Roles = "Member"});
}

Continue reading

ASP.Net MVC 5 – Attribute Based Routing Part 1

Attribute Based Routing:

Routing is a process of mapping a virtual or nonexistent URL to a physical resource on server. Url Routing is not a new feature and traces of routing were always available in asp.net web forms from version 2.0.

If you have worked on web forms in asp.net 2.0 than you must remember the attribute called UrlMapping in web.config. Just to recap how that worked

<system.web>
    
    <urlMappings>
      <add url="~/cricket" mappedUrl="~/Home/Cricket.aspx"/>
    </urlMappings>
</system.web>

Continue reading

ASP.Net MVC 5 – Attribute Based Routing Part 3

Overriding a Route Prefix

We can override a Route prefix applied at controller level by using “~” character with Route attribute

[RoutePrefix("Sports")]

public class HomeController : Controller

{

[Route("{category}")]

public ActionResult SomeAction(string category)

{

ViewBag.category = category;

return View();

}

[Route("~/Games/{category}/edit")]

public ActionResult SomeMoreAction(string category)

{

ViewBag.category = category;

return View();

}

}

Continue reading

ASP.Net MVC 5 – Attribute Based Routing Part 5

Route Constraints

We can restrict the parameters used in routes via Route constraints. There are many route constraints provided by Asp.net mvc and you can create your custom constraints as well:

Examples:

//Sports/SomeAction/category

[Route("{category:int}",Name = "Test")]

public ActionResult SomeAction(int category)

{

ViewBag.category = category;

return View();

}

Continue reading