ASP.NET MVC Overview


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?

  1. Incoming request redirected to controller.
  2. Controller process the request and forms a data model.
  3. Model is passed to View.
  4. View transforms Model into appropriate output format.
  5. 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

Controller

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.

  1. Action method should return an ActionResult.
  2. ViewResult - if you want to show a view.
  3. ContentResult - Directly return content.
  4. RedirectResult to redirect to a new URL.
  5. RedirectToRouteResult - represents a redirection to a new controller action.
  6. EmptyResult - null object pattern.
  7. JsonResult - returning a blob of Json.
  8. JavaScriptResult - script that can be executed on the client.

A controller only contains the logic required to return the right view or redirect the user to another action.

View

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.

Model

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.

Up Next
    Ebook Download
    View all
    Learn
    View all