ASP.NET MVC 5: New Features

Introduction

sonia jessica
C# Programming

--

Today, people all over the world use numerous web applications in their daily lives. Many of these applications are boring, tedious, and not secure. This is where ASP.NET comes into play and makes these applications valuable.

The ASP.NET MVC framework has developed into a popular choice among developers for building all types of web applications. It is easier to develop, debug, and deploy ASP.NET MVC web apps. All of these tasks can be handled within Visual Studio .NET, an integrated development environment.

Several of the world’s largest companies have their heavily trafficked websites built on ASP.NET MVC, including Microsoft, Volvo, Stack Overflow, Ikea, etc.

What is ASP.NET MVC 5?

Based on Model-View-Controller (MVC) architecture, ASP.NET MVC 5 is a web development framework that supports programming models, complete software infrastructures, and various services for the creation of powerful web applications for mobile devices and PCs. By using HTML, CSS, and JavaScript, ASP.NET allows developers to create web applications. It was released by Microsoft in 2009 and is a part of the Microsoft .Net platform. ASP.NET applications employ compiled codes written with reusable and extensible .Net framework components and objects. These codes can utilize all the classes in the .Net framework. HTML/JS, WML, XML, VB.NET, C#, J#, JScript.NET, and Visual Studio are all supported by the framework.

Now some of you must be thinking- What does MVC mean?

In the field of software and web development, “Model-View-Controller (MVC)” is a term you hear a lot, but if you’re unfamiliar with it, it can be confusing.

Model-View-Controller is essentially a design pattern that provides code reusability and a more structured application architecture by separating the different aspects of a piece of Software.

A website, for example, could be broken down into three main components.

  • Firstly, the “View” is what the user sees and is usually composed of HTML, CSS, and Javascript
  • Secondly, the “Model” refers to how the website interacts with the database.
  • Furthermore the “Controller” connects the “View” and “Model” together. The “Controller” brings a request from the “View” and dispatches it to the “Model” when a user requests something from the database.

A real-world example of the Model-View-Controller (MVC) model can be picturized through a Car driving mechanism:

Three main components make up every car:

  • The View is illustrated by-User interface which consists of the brake, Gear lever, steering wheel, and much more elements of a car.
  • The Controller is depicted by-Mechanism (Engine of the vehicle)
  • Model is depicted by-Storage (fuel tank of the vehicle)

ASP.NET MVC 5 new features:

1. One ASP.NET
One ASP.NET provides web developers with a unified project system. With this system, you can work with numerous frameworks, such as MVC, Web API, Web Forms, etc., in one project. Using the One ASP.NET project system, as a result, you will be able to use ASP.NET Web Forms and MVC jointly and, if desired, ASP.NET Web API and SignalR, as well in the same Web application.

Employing One ASP.NET Experience to Create a New Site:

  • First, open the Visual Studio Express 2013 for Web
  • From the file menu, select new project.
  • Now after selecting a new project, choose ASP.NET Web Application and click OK.
  • After performing that, select the Web Forms template and select the MVC and Web API checkboxes.
  • Authentication option should be set to Individual User Accounts and after performing that select OK.

This is how you can start a new project and add Web API and MVC components utilizing the Web Forms template.

2. Bootstrap:

Twitter Bootstrap has been added as the default user interface framework for MVC applications in Visual Studio 2013. Twitter created Bootstrap to design forms, navigation, buttons, tables, etc. with HTML and CSS. Bootstrap is used for developing Rapidly Responsive User Interfaces by leveraging HTML and CSS-based templates.

Bootstrap can be used to create an interface that performs well across all screen sizes, including devices that we use for day-to-day activities like desktops and mobile devices.

Commencing a new MVC project will include Bootstrap, so you will see Bootstrap.css and Bootstrap.js in your project.

2a. Attribute-Based Routing

ASP.NET MVC routes a URI to action through routing. In MVC 5, there is a new type of routing called attribute routing. The method depicts routes that are based on attributes. Using attribute routing in your web application will give you greater control over URIs.

There is still support for the earlier style of routing, known as convention-based routing. Moreover, both techniques can be combined within a single project.

During configuration, call MapMvcAttributeRoutes in the RouteConfig file to enable attribute routing.

SYNTAX:

public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapMvcAttributeRoutes();
}
}

2b. Scaffolding

In many MVC frameworks, such as ASP.NET MVC, scaffolding is employed to generate code for primary CRUD (create, read, update, and delete) operations against your database. The auto-generated code can also be edited or customized to meet your needs. Scaffolding can help to diminish the length of codes that you wrote manually in the earlier versions of MVC.

Among scaffolding’s components are page templates, entity page templates, field page templates, and filter templates. This type of template is called a scaffold template and is very useful as it facilitates you to quickly create a data-driven website.

We will further see how to implement it in the project discussed later in this article.

2c. Filter Overrides

In previous versions of MVC, you had to apply a filter for every action and controller individually in order to override a filter. A new feature of MVC 5 is Filter Overrides. According to the documentation “Overriding which filters apply to an action method or a controller can now be accomplished by defining a filter override. An override filter will define a set of filter types that should not be applied to a provided action or controller. This allows you to add on global filters, but exclude some from a certain action or controller”.

With MVC, the following filters are available:

  • Authentication filters
  • Exception filters
  • Action filters
  • Result filters
  • Authorization filters

Accordingly, we have five filter overrides

  • OverrideAuthenticationAttribute
  • OverrideExceptionAttribute
  • OverrideAuthorizationAttribute
  • OverrideActionFiltersAttribute
  • OverrideResultAttribute

The Authorize Filter is applied at the controller level in the following example. Thus, only the Admin user can access the Home controller action methods. The Authorize Filter can now be excluded or bypassed from the “About” method.

In the following case, this “About” method can be marked with the “OverrideAuthorization” attribute. The Admin user can now access all action methods of the home controller except the “About” method. “About” can be accessed without authorization.

3. Understanding and Implementing the ASP.NET MVC 5 features in Real-Time project

The Library Management System will be created using ASP NET and is used for keeping track of all library management activities, such as recording book stocks, all the student information, issuing books, returning the books.

By Building this project you will learn:

  • How to create a data model
  • How to scaffold a basic crud application for your model.
  • How does ASP.Net MVC handles all the requests efficiently
  • How to perform Routing

The functionality of a librarian includes:

  • It should be possible for him to add new books to the library.
  • The list of all the books and books borrowed should also be known to him
  • Viewing the history of books borrowed by customers.
  • It should be possible for him to edit the book details.
  • It should be possible for him to remove a book.
  • Customers can be added to the list.
  • Assigning a borrowed book to a customer should be another possible task for him.

As discussed before also, you can commence a new project and add Web API and MVC components using the Web Forms template.

1) First, open the Visual Studio Express 2013 for Web
2)Open the file menu and then select new project.
3)Now select ASP.NET Web Application and click OK.
4) After that, select the Web Forms template and select the MVC and Web API checkboxes.
5) Authentication option should be set to Individual User Accounts and after that select OK.
In the solution explorer, open the Models folder.

Create a new class called management in the Models folder. This can be done by right-clicking on the Models folder, choosing add and clicking class.

This is how your management class should look after you update the code.

public class Management
{
public int BId { get; set; }
public string Book_Tittle
{ get; set; }
public string No
{ get; set; }
public string Writer
{ get; set; }
public string Published_By
{ get; set; }
}
}

To map our textbook model to our database, we need to add it to the ApplicationDbContext.

As you work you are scaffolding a basic CRUD operation for books, namely Create, Read, Update, and Delete.

Our navigation bar will possess a link to books. This is done by using Html.ActionLink takes three parameters. The first parameter is the name that will appear in the link, the second parameter is the Action within our controller, and the third parameter is the name of the controller.

Run the application after you’ve added the link to our textbook.

In the navigation bar, you should now see the link for Textbooks. Click on it, and this page will appear. On this page, you will find the book controller’s index.

Now, Understanding the request lifecycle in ASP.Net MVC is crucial to building this project.

Our MVC application looks for the routes registered in the URL Routing Module for which controller to call when making HTTP requests.

In the App_Start folder, open the RouteConfig.cs file to see the registered routes

Compare the value of URL in routes.MapRoute with our browser’s URL.

To determine which controller will handle a request from the above URL, it will first inspect the registered routes. When the controller is determined, the module will initialize the controller and determine what action should be executed for the request with a module called Action Invoker.

The Edit method can be found in the code of the BooksController.

The view-engine will find the necessary view in the Views folder after performing the action.In our case it will be looking for Views -> Books -> Edit.cshtml. After that view will be rendered for the user.

For building the complete project you can find the source code here.

Conclusion

So in this article, we discussed some new and interesting features of ASP.NET MVC 5 to make your coding journey easy. Earlier versions of MVC could have provided some of the functionality provided by these new features, but now that these features are part of MVC, it is easier than ever before to use them since there are no configuration changes or external components needed. Hope you find this article helpful.

--

--