Week 2 Lecture 3 Theory
Week 2 Lecture 3 Theory
Net MVC
Course Code: CSC 4164 Course Title: ADVANCED PROGRAMMING WITH .NET
All routes have an associated route handler with them, and this is the entry point to
the MVC framework.
Creating MVC application
Step 1: Open Visual Studio Step 2: Click on New Project to create a web
application
Step 3: Select the MVC Template Step 4: Change authentication
Routing is the process of directing an HTTP request to a controller and the functionality of
this processing is implemented in System.Web.Routing. The Global.asax file is where you
will define the route for your application.
namespace MVCFirstApp
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
}
Following is the implementation of RouteConfig class, which contains one method
RegisterRoutes. Define routes to map URLs to a specific controller action. ASP.NET
MVC application uses routing rules defined in Global.asax to find out the
appropriate Controller and pass the request.
namespace MVCFirstApp
{
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 }
);
}
}
}
Controllers and Action Methods
Controllers
In ASP.NET MVC, a Controller defines and groups a set of actions. The activities performed
by the controller are given below:
• Calls necessary Model that will interact with database and fetch data back to
Controller.
• Controller further calls required View and pass the data which is further rendered to
client as a response of the request.
Controller Model
request data
response data
View Database
Action Methods
• Action Methods are public methods inside a controller class.
• A Controller class can have multiple action methods that are responsible
for performing certain operations depending upon the client action.
• Actions can return anything but often returns an instance of IActionResult
that produces a response.
ProcessController has only one default Action Method called Index
Controller
Action method
Different response types
Action Result Helper Method Short Description
RedirectToAction or
RedirectToRouteResult It redirects to another action method.
RedirectToRoute
• Razor View Engine has .cshtml (with C#) and .vbhtml (with VB)
extension for views.
Different view engines
• Razor
• Spark
• NHaml
• NDJango
• Hasic
• Brail
Books