SecondLecture
SecondLecture
View
Views folder contains HTML files for the application.
Typically view file is a .cshtml file where you write html and C# or VB.NET code.
Views folder includes separate folder for each controllers. For example, all the
.cshtml files,
which will be rendered by HomeController will be in View > Home folder.
Shared folder under View folder contains all the views which will be shared
among different controllers e.g. layout files.
Global.asax
Global.asax allows you to write code that runs in response to
application level events, such as Application_BeginRequest,
application_start, application_error, session_start, session_end etc.
Packages.config, Web.config
Packages.config file is managed by NuGet to keep track of what
packages and versions you have installed in the application.
Web.config file contains application level configurations.
Routing in MVC
In the ASP.NET Web Forms application, every URL must match with a specific .aspx file.
For example, a URL http://domain/studentsinfo.aspx must match with the file studentsinfo.aspx
that contains code and markup for rendering a response to the browser.
ASP.NET introduced Routing to eliminate needs of mapping each URL with a physical file.
Routing enable us to define URL pattern that maps to the request handler.
This request handler can be a file or class.
In ASP.NET Web form application, request handler is .aspx file and in MVC, it is Controller class
and Action method.
Route
Route defines the URL pattern and handler information.
All the configured routes of an application stored in RouteTable and will be used by
Routing engine to determine appropriate handler class or file for an incoming
request.
Configure a Route
Every MVC application must configure (register) at least one route, which is configured
by MVC framework by default.
You can register a route in RouteConfig class, which is in RouteConfig.cs under App_Start
folder.
URL Pattern
The URL pattern is considered only after domain name part in the URL.
For example, the URL pattern "{controller}/{action}/{id}" would look like
localhost:1234/{controller}/{action}/{id}.
Anything after "localhost:1234/" would be considered as controller name.
The same way, anything after controller name would be considered as action name and then
value of id parameter.
Multiple Routes
You can also configure a custom route using MapRoute extension method.
You need to provide at least two parameters in MapRoute, route name and URL pattern.
The Defaults parameter is optional.
You can register multiple custom routes with different names.
Custom Routes
Register Routes
Now, after configuring all the routes in RouteConfig class, you need to
register it in the Application_Start() event in the Global.asax. So that
it includes all your routes into RouteTable.
Controller
The Controller in MVC architecture handles any incoming URL request.
Controller is a class, derived from the base class System.Web.Mvc.Controller.
Controller class contains public methods called Action methods.
Controller and its action method handles incoming browser requests, retrieves necessary model
data and returns appropriate responses.
In ASP.NET MVC, every controller class name must end with a word "Controller".
For example, controller for home page must be HomeController
Also, every controller class must be located in Controller folder of MVC folder structure.
Adding a New Controller
Scaffold Dialog
Scaffolding is an automatic code generation framework for ASP.NET web applications.
Scaffolding reduces the time taken to develop a controller, view etc. in MVC framework.
Adding a New Controller
Action Method
All the public methods of a Controller class are called Action methods.
They are like any other normal methods with the following restrictions:
1. Action method must be public.
2. It cannot be private or protected.
3. Action method cannot be overloaded.
4. Action method cannot be a static method.
Default Action Method.
Every controller can have default action method as per configured route in
RouteConfig class.
By default, Index is a default action method for any controller, as per configured
default route.
Default Action Method.
Action Result
MVC framework includes various result classes, which can be return
from an action methods.
There result classes represent different types of responses such as
html, file, string, json, javascript etc.
The END!