ASP. net mvc overview and the first MVC program, asp. netmvc

Source: Internet
Author: User

ASP. net mvc overview and the first MVC program, asp. netmvc

I. ASP. NET Overview
1.. NET Framework and ASP. NET
. NET Framework contains two important components:. NET Framework class library and public language. Compile ASP. NET
When the. NET Framework class library and public language are used on the page
2. Introduction to ASP. NET MVC
ASP. net mvc is a subset of ASP. NET technology. It is a perfect combination of ASP. NET technology and MVC mode, similar to Java
Struts Framework of the platform
3. features and advantages of ASP. NET
1. It is developer friendly and highly efficient in development
2. Convenient breakpoint setting and easy debugging
3. Compile and execute the program, and the running efficiency is high.

2. Create the first ASP. net mvc application

  

 


1. The project structure is described as follows:
App_Data Folder: used to store database files contained by the System
App_Start Folder: contains files related to ASP. net mvc system startup.
Controllers Folder: place the Controller code file of the entire project
Models Folder: place the model code file of the entire project.
Views Folder: place the view code file of the entire project
Web. config file under Views: the configuration file acting on the View
Global. asax file: Global application file, which is generally used together with classes in the APPStart folder.
Packages. config file: used to manage the Assembly versions used by the Project
Web. config in the root directory: the configuration file for the entire project

 


2. Create a controller
Right-click the "Controllers" folder in the solution and select "add"> "controller" from the shortcut menu.
Open the "add controller" dialog box
Eg:

    

1 public class HomeController : Controller2                 {3                     public ActionResult Index()4                     {5                         return View();6                     }7                 }

 



Index () method: The Action Method in the controller, used to respond to client requests and call the response View to the browser
Output Information
3. Create and compile a view
Create a "Home" folder in the "Views" folder and create a view under "Home ".
4. Working Principle of ASP. net mvc Program
1. The Global. asax file is mainly used for Global initialization during system startup and calls the App_Start folder.
Eg:

 1      public class MvcApplication : System.Web.HttpApplication 2                     { 3                         protected void Application_Start() 4                         { 5                             AreaRegistration.RegisterAllAreas(); 6                             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 7                             RouteConfig.RegisterRoutes(RouteTable.Routes); 8                             BundleConfig.RegisterBundles(BundleTable.Bundles); 9                         }10                     }        

 


2. RouteConfig. cs
Eg:

 1  public class RouteConfig 2                     { 3                         public static void RegisterRoutes(RouteCollection routes) 4                         { 5                             routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 6  7                             routes.MapRoute( 8                                 name: "Default", 9                                 url: "{controller}/{action}/{id}",10                                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }11                             );12                         }13                     }

 



Note: "{controller}/{action}/{id}" is actually a defined URL rule.
Use http: // localhost: 1543/Home/Index/0 to access the system.
5. View naming and addressing rules (in the controller, use the View () method to call the View and return the View with the same name as the action method)
A major feature of the ASP. net mvc framework is that "conventions are better than configurations", that is, some rules are agreed and do not need to be configured through the configuration file.
All controllers end with the Controller.
All Views in the application have a unified home directory of Views.
Subdirectory with the same name as the controller in the home directory of Views


Iii. Web Server (IIS Express and Development Server)
If the ASP. NET program is officially released, it needs to run on another Web Server IIS

1. How to debug ASP. net mvc program?
You need to configure in Web. config under the root directory: <compilation debug = "true" targetFramework = "4.5"/>
Debug = "true" indicates debugging is supported. After the official release, debug must be set to false.
Iv. ASPX View
1. ASPX view declaration and encoding
(1) common attributes of @ Page commands
Language: Specifies the Language used by the Page code and the post code.
Inherits: the inherited page class. The ASPX view generally Inherits System. Web. Mvc. ViewPage.
ContentType: Specify the MIME type and the character encoding method used for page response.
ValidateRequest: Specifies whether to verify the request
MasterPageFile: Specifies the master view file to be used.
(2) @ Import command (introduce namespace)
<% Import Namespace = "MvcDemo. Helpers" %>
(3) server-side embedded syntax
1) small script and Expression
Eg:

        

1 <% 2 // calculate the maximum value of 3 int [] values = {25, 30, 54,20}; 4 int max = 0; 5 foreach (int value in values) {6 if (value> max) 7 max = value; 8} 9%>

 


2). server script block (some methods and global variables can be defined)
Eg:

1 <script runat = "server"> 2 string message = "display maximum value:"; 3 int GetMax (int [] values) 4 {5 int max = 0; 6 foreach (int value in values) 7 {8 if (value> max) 9 max = value; 10} 11 return max; 12} 13 </script> 14 <% = message + getMax (new int [] {,}) %>

 


ASPX View Expression <% = %> can also be written as <%: %>

2. Master View
1. Create a master View
The master view is usually placed in a fixed folder named "Shared" under Views. In this folder, you can
Right-click "add" "new item" in the shortcut menu and choose "open new item"> "MVC4 view master ".
2. Special Features of the master view and common view
(1). The beginning of the page is the @ Master command, which also has the corresponding attributes, including the Language and Inherits attributes. It defaults
The inherited base class is System. Web. Mvc. ViewMasterPage.
(2) The page contains two <asp: ContentPlaceHolder/> tags, which indicate the content of the page
Container and location of the content View
3. Use the template View
To create a content view that uses the template view, you only need to check the "use layout or master page" check box and select the created content view in advance.
Master View

(1). @ Page command declaration adds the MasterPageFile attribute to the path of the master View File.
(2). </asp: Content> tag, which corresponds to the </asp: ContentPlaceHolder> tag in the master view.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.