ASP.NET MVC study notes-1.ASP.NET MVC basics

  ASP.NET MVC abandons the page-based architectural style on the basis of the original ASP.NET, and uses a new MVC (Model-View-Controller) architecture technology.

  Currently, it and ASP.NET coexist on top of the .NET Framework.

1.       MVC Architecture

  The MVC architecture is an architectural pattern that strictly implements the isolation of various parts of the application, which is called "loose coupling". The benefits of this architectural pattern are as follows:

  1) Development

    The development of a single component no longer depends on other components, reducing the impact between components.

  2) Test

    The loose coupling between components allows the test code to replace other components, reducing other unnecessary operations and simplifying the testing process.

  3) Maintenance

    The dependencies between components are reduced, and when one component is modified, the impact on other components is minimized, which is more conducive to maintenance.

2.       The composition of MVC architecture

  The MVC architecture strictly divides the application into three layers: model, view and control. Each layer has its own responsibilities and does not need to pay attention to the specific implementation of other layers. The relationship between the three layers is shown in the following figure:

 

  Model: It represents the core business logic and data, and encapsulates some properties and behaviors.

  View (View): Responsible for transforming and presenting model data on the presentation layer.

  Controller: Controls the program logic and coordinates the relationship between the view and the model. It accepts requests from the view, uses the model to perform corresponding operations, and feeds back the executed structure to the view.

3.       Routing

  Requests to ASP.NET MVC websites are all in the form of URLs. Therefore, the system can make corresponding actions according to the URL information. This is the main function of the ASP.NET Routing module. Therefore, ASP.NET Routing is ASP.NET The core of MVC requests.

Simply put, ASP.NET Routing is a pattern matching system. When the program is initialized, it performs pattern registration according to the information in the Route table, that is, it tells the Routing module how to process requests that match the pattern. When the program is running, the Routing module accepts the request and first The routing table performs pattern matching. When the matching is successful, the request is forwarded to a specific controller for processing. When the matching is unsuccessful, a 404 error code is returned.

4.       Routing configuration

  When creating a program, ASP.NET routing has been configured by default, mainly including two locations

  First, configuration sections related to routing are configured in the Web.config file, <system.web.httpModules/>, <sytem.web.httpHandlers/>, <system.webserver.modules/>, <sytem.webserver.handlers/ >.

  Secondly, the registration information called when the program starts, the file location App_Start/RouteConfig.cs, and the extension method MapRoute() method is called.

    routes.MapRoute(

                 "Default",//Route name

                 "{controller}/{action}/{id}",//URL parameter information

                 new {controller=””, action=””, id=UrlParameter.Optional }//Default URL parameter

  It is also possible to add custom routes, however, the code location must be placed before the default route, otherwise it will never be matched.

URL

Controller

Action

ID

 

/auctions/auction/1234

AuctionsController

Auction

1234

perfect match

/auctions/recent

AuctionsController

Recent

 

no parameters

/acutions

AuctionsController

Index

 

Default Action

/

HomeController

Index

 

system default

 

5.       Controller

  In the MVC architecture, the Controller can respond to the request sent by the view, obtain the corresponding model data, and feed it back to the view. In an ASP.NET MVC program, Controller is the class that contains the methods that are called when a request is processed by the routing module.

  Controller is a class, uniformly derived from the System.Web.Mvc.Controller base class.

  ASP.NET MVC program has a convention that the created Controller class must end with Controller, such as HomeController. When the system starts, it will automatically search for subclasses derived from Controller and instantiate the class whose name is suffixed with Controller. When using the controller in the program, you do not need to fill in the following Controller suffix, just use the name directly.

6.       ControllerAction

  The public method on the Controller class is called ControlAction. ControlAction cannot be overloaded, nor can it be a static method.

7.       ActionResult

  After ControllerAction processes the request, if it needs to return the processing result, it needs to use ActionResult to return it. Each ControllerAction in the Controller must return ActionResult, but it does not need to be manually coded, just call the corresponding method in the Controller base class.

Content()

Returns a ContentResult of type text

“Hello World”

File()

Returns a FileResult of the file contents

 

HttpNotFound()

Returns HTTPNotFoundResult with 404 error code

 

JavaSript ()

Returns a JavaScriptResult containing the JavaScript content

“function hello(){

 alert(hello world);

}”

Json()

Returns JsonResult of data in Json format

 

PartialView()

Returns a PartialViewResult containing the content of the partial view

 

Redirect()

Jump to the given URL

 

View()

Returns the ViewResult of the rendered view

 

8.       Operating parameters

  When the ControllerAction is executed, it can use the parameter information sent during the request. This function is called "model binding". for example:

  ublic ActionResult Index(string name,string password)

  {  

  }

  When the ASP.NET MVC framework executes this method, it will automatically assign the passed parameter value to the corresponding parameter. The ASP.NET MVC framework handles a lot of the groundwork, allowing us to focus on the business logic, and the code is more readable.

9.       View

  The ControllerAction returns a ViewResult instance of type ActionResult to the view. When rendering the view, the ASP.NETMVC framework will use the names of Controller and ControllerAction. Take Index in HomeController as an example:

  public ActionResult Index()

  {

           return View();

  }

  A convention in the ASP.NET MVC framework, the view files used in the Controller need to be placed in a folder named after the Controller name under Views. Moreover, if the view name parameter is not passed in the return value, the name of the ControllerAction is directly used as the file name of the view. If the corresponding view file is not found, it will continue to search in the Shared folder under Views.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325069683&siteId=291194637