The MVC design pattern separates the application logic from the user interface. This allows us to do development, testing and maintenance independently. When building an internet site in which HTML, performance and scalability are important, ASP.NET MVC is a better choice.
How MVC works?
- Incoming request redirected to controller.
- Controller process the request and forms a data model.
- Model is passed to View.
- View transforms Model into appropriate output format.
- Response is rendered.
URL in MVC Application
When we request a page of web forms applications, there is a one-to-one correspondence between a page and URL. If you request a page named Test.aspx from the server, there had better be a page on that server. If the file doesn't exist, we will get a 404 (page not found) error.
When building an ASP.Net MVC application, there is no correspondence between the URL and page. But in ASP.Net MVC, an URL corresponds to a Controller action.
What is URL Routing?
A browser gets mapped to a control action through a feature of ASP.Net MVC application called URL routing. URL Routing routes incoming request to controller actions. URL routing uses the route table to handle incoming request. This route table is created when your Web application first starts. The route table is setup in the Global.Asax file.
When an ASP.Net application first starts, the Application_Starts() method is called. This method calls the RegisterRoutes() method and the RegisterRoutes() method creates the default route table.
The default route table consists of one route. The default route breaks all incoming requests into three segments. The first segment is mapped to a controller name, the second segment is mapped to an action name, and the final segment is mapped to a parameter passed to the action name Id (optional).
Role of Controller, View and Model
A controller is responsible for controlling the way that a user interacts with an MVC application. A Controller determines what response to send back to user when a user makes a browser request. Role of controller is to load the model and call the view. The default controller is HomeController, the default Action is Index, and the default Id is an empty string.
- Action method should return an ActionResult.
- ViewResult - if you want to show a view.
- ContentResult - Directly return content.
- RedirectResult to redirect to a new URL.
- RedirectToRouteResult - represents a redirection to a new controller action.
- EmptyResult - null object pattern.
- JsonResult - returning a blob of Json.
A controller only contains the logic required to return the right view or redirect the user to another action.
A view contains the HTML markup and content that is sent to the browser. We must create the view in the right location. A view should contain only logic related to generating the user interface. All pages in ASP.Net MVC inherit from VIEWPAGE. VIEWDATA helps us to send the model data to view.
An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic and database access logic.