Updated March 15, 2023
Introduction to ASP.NET MVC 4
ASP.NET MVC 4 is an Open-Source Software Framework for creating extremely scalable, testable, standard-based, and controllable Web Applications that follow the Model-View-Controller (MVC) pattern. It supports a clear separation of concerns like View for UI, Controller for handling user data, and Models for the logical domain. By using this, developers can easily build web apps using various features, allowing rapid application development and clean separation of code.
What is ASP.NET MVC?
ASP.NET MVC is the Framework that combines the features of the MVC (Model-View-Controller) Pattern with an updated system. MVC Framework is used to create Web Applications by using the three components as
- Model- represents the data access called the business layer
- View- represents the UI part of the application
- The controller handles the user request; it controls the entire application.
Create new ASP.NET MVC 4 projects
ASP.NET MVC controls the application problem by dividing an application into three components: Model, View, and Controller. Let’s see the creation of the MVC Application as follows,
Initially, go to FileàNewàProject. Once the dialog opens under the Web Template, choose the ASP.NET MVC 4 Web Application, give the project’s right name, and click ok.
Next, select the template for your project; here, I’m selecting the Empty Template and View Engine as Razor as shown below,
Once selecting the Empty Template, the Folder Structure of the project looks as follows, there will be three architectural components like Model, View, Controller,
Next, to include the Controller in the Project, the entire request by the user is handled by the Controller. To add Controller, right-click on the Controller Folder and Addà Controller.
Once adding the Controller, give the Controller’s name; every controller name must end with Controller. Here the name of the Controller will be HomeController,
The HomeController contains the default Index method as follows,
We can alter the Index Action Method as we wish to return a string; give the return message like “Welcome to ASP.NET MVC-4”.
public class HomeController : Controller
{
public string Index()
{
return "Welcome to ASP.NET MVC-4";
}
public string About()
{
return "Welcome to ASP.NET MVC-4 – About Page";
}
}
To execute the application, the result will be as follows,
We can also give the url directly by entering the Controller and Method name to proceed with it,
E.g., http://39393/home/index, we get the same result while entering it. If
we want to go specific page, we can also enter like http://39393/home/about. The following result will be displayed,
We can get the specific page directly without entering in website by specifying it in the RouteConfig.cs file in the project itself. The class used for URL Mapping which presented in the App_Start Directory; let’s see the RouteConfig.cs file
The RouteConfig.cs contains the url pattern like as given url:”{controller}/{action}/{id}” it defines initially the controller to be passed and the name of the action method followed by a slash (/) and finally the optional parameter. The RouteConfig.cs code will be as follows,
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
ASP.NET MVC 4 on server Installing
To install ASP.NET MVC 4, download and install Visual Studio 2010 Service Pack 1it require using MVC 4 in VS 2010. Just download from Microsoft Download Centre or follow the link below,
http://www.microsoft.com/en-in/download/details.aspx?id=23691
“VS10sp1-KB983509.exe” will be downloaded. Once installing VS 2010 SP 1, proceed with installing the MVC, http://www.microsoft.com/en-us/download/details.aspx?id=30683
Once downloading the setup file “AspNetMVC4Setup.exe” MVC 4 is installed, double click on it to execute.
Once installed, the below screen comes finally.
ASP.NET MVC 4 Project Templates
ASP.NET MVC supports a clear separation of concerns like View for UI, Controller for handling user data, and Models for the logical domain. By using this, developers can easily build web apps using various features, allowing rapid application development and clean separation of code. Let’s see the various templates available in this project as follows,
Empty Template – contains the basic empty MVC Folder Structure with Global. asax file and the App_Start Folder at the top. It will contain the Script and Content Folder. The template looks as follows,
Basic Template comes with MVC Folders empty, but the View Folder contains the Shared Folder with Layouts.cshtml and Error.cshtml. It contains default Script files, and the content folder contains the themes and style sheet. When selecting the basic template, the folders structure looks as follows,
Internet Application Template comes with the folder structure HomeController, and the AccountController with default View and the AccountModels require registration. It default comes with basic login and registration mechanism built into it. The Folder Structure looks as follows,
Intranet Application Template – it is similar to an internet application template. It contains the default HomeController with View and comes with a Windows-Based-Authentication Mechanism. The template looks as follows,
Mobile Application Template is similar to the internet application template with Home and Account Controller with default Views. This template comes along with the Registration and Login mechanism. We can remove DotNetOpenAuth references for external log functions. The template looks as follows,
Web API Template is another version of the internet Application template; it comes with Home and Value Controller with default View for HomeController, and the ValueController inherits from the ApiController. It is mainly used to create HTTP RESTful Web Services. The template folder structure looks as follows,
ASP.NET MVC 4 New Features
In ASP.NET MVC 4, there will be a new feature available. Let’s see the features of ASP.NET MVC,
Bundling – It is used to improve the request load time. It contains the code below, which is present in the “App_Start\BundleConfig.cs” file.
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
}
We can add multiple RegisterBundle methods to the file as shown below,
bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
"~/Content/themes/base/jquery.ui.core.css",
"~/Content/themes/base/jquery.ui.resizable.css",
"~/Content/themes/base/jquery.ui.selectable.css",
"~/Content/themes/base/jquery.ui.accordion.css",
"~/Content/themes/base/jquery.ui.autocomplete.css",
"~/Content/themes/base/jquery.ui.button.css",
"~/Content/themes/base/jquery.ui.dialog.css",
"~/Content/themes/base/jquery.ui.slider.css",
"~/Content/themes/base/jquery.ui.tabs.css",
"~/Content/themes/base/jquery.ui.datepicker.css",
"~/Content/themes/base/jquery.ui.progressbar.css",
"~/Content/themes/base/jquery.ui.theme.css"));
Mobile Development – we can create the mobile page in MVC using the mobile jQuery and HTML 5.
Oauth Providers – ASP.NET MVC provides users to log credentials with external providers. There will be various OAuth providers like Facebook, Twitter, etc.
Conclusion
This article taught us about ASP.NET MVC 4 and creating new MVC 4 projects with their different project template folder structures. Hope the article helps you to understand.
Recommended Articles
We hope that this EDUCBA information on “ASP.NET MVC 4” was beneficial to you. You can view EDUCBA’s recommended articles for more information.