ASP. NET MVC Overview and the first MVC program

Source: Internet
Author: User

I. Overview of ASP.
1. The. NET Framework and ASP .
The. NET Framework contains two important components: when a. NET Framework class library and a common language are in progress. Write an ASP.
When a page needs to use the Framework class library and the common language of the. NET Framework
2. Introduction to ASP.
ASP. NET MVC is a subset of ASP. ASP, which is a perfect combination of the net technology and the MVC pattern, similar to Java
The struts framework of the platform
features and advantages of 3.asp.net
1. Developer-friendly, high-efficiency development
2. Easy to set breakpoints, easy to debug
3. Post-compilation execution, high operating efficiency

Ii. Creating the first ASP. NET MVC application

  


1. The project structure is described as follows:
App_Data folder: Used to store database files contained in the system
App_start folder: Related class file containing ASP.
Controllers folder: The controller code file that puts the entire project
Models folder: A model code file to place the entire project
Views folder: A View code file that places the entire project
Web. config file under Views: A configuration file that acts on a view
Global.asax files: Global application files, common with classes in the AppStart folder
Packages.config file: The version of the Assembly used to manage the project
Web. config in the root directory: a configuration file that acts on the entire project


2. Create a controller
Right-click the "Controllers" folder in the solution and select "Add"--"controller" option from the popup shortcut menu
To open the Add Controller dialog box
eg

    

1  Public class Homecontroller:controller 2                 {3public                       actionresult Index ()4                    {  5                         return  View (); 6                     }7                 }



Index () Method: An action method in the controller that responds to a client request and invokes a view of the response to the viewer
Output information
3. Creating and Writing views
Create a "Home" folder in the "Views" folder, under "Home" to create a view
how the 4.asp.net MVC program works
1.global.asax file is mainly to do some global initialization when the system starts, call the App_start folder
eg

1       Public classMvcApplication:System.Web.HttpApplication2                     {3                         protected voidApplication_Start ()4                         {5 Arearegistration.registerallareas ();6 filterconfig.registerglobalfilters (globalfilters.filters);7 routeconfig.registerroutes (routetable.routes);8 bundleconfig.registerbundles (bundletable.bundles);9                         }Ten}


2.routeconfig.cs
eg

1   Public classRouteconfig2                     {3                          Public Static voidregisterroutes (routecollection routes)4                         {5Routes. Ignoreroute ("{Resource}.axd/{*pathinfo}");6 7 routes. MapRoute (8Name"Default",9Url:"{Controller}/{action}/{id}",TenDefaultsNew{controller ="Home", action ="Index", id =Urlparameter.optional} One                             ); A                         } -}



Note: "{controller}/{action}/{id}" is actually a defined URL rule
Using http://localhost:1543/Home/Index/0 to access the system, the system will automatically correspond to
5. Rules for view naming and addressing (in the controller, call the view using the view () method, return the view with the same name as the action method)
One of the features of the ASP. NET MVC framework is that "conventions are better than configuration", that is, some rules are agreed, and do not need to be configured by configuration files.
All controllers end With controller
All views in the application have a unified view home directory
Subdirectories with the same name as the house and controller in the Views home directory


iii. Web Server (IIS Express and Development Server)
If you are publishing an ASP. NET program, you need to run it on a different Web server IIS

1. How do I debug an ASP. NET MVC program?
Need to be configured in Web. config in the root directory: <compilation debug= "true" targetframework= "4.5"/>
where debug= "true" means debugging is supported, and debug needs to be set to false after the official release
Four, aspx views
declarations and encodings for 1.ASPX views
(1) Common properties of @Page directives
Language: Specify the language used by the page code and the post code
Inherits: Inherited page class, aspx view is generally inherited by System.Web.Mvc.ViewPage
ContentType: Specifies the character encoding used for MIME types and page responses
ValidateRequest: Specifies whether to validate the request
MasterPageFile: Specify the master view file to use
(2), @Import directives (introducing namespaces)
<% Import namespace= "Mvcdemo.helpers"%>
(3), server-side inline syntax
1), small scripts and expressions
eg

        

1<%2                                 //calculate the maximum value3                                 int[] values={ -, -, Wu, -};4                                 intmax=0;5                                 foreach(intValueinchvalues) {6                                     if(value>max)7max=value;8                                 }9%>


2), server-side script block (you can define some methods and global variables)
eg

1<script runat="Server">2                                 stringMessage="Display Maximum value:";3                                 intGetmax (int[] values)4                                 {5                                     intmax=0;6                                     foreach(intValueinchvalues)7                                     {8                                         if(value>max)9max=value;Ten                                     } One                                     returnMax; A                                 } -</script> -<%=message+getmax (New int[]{ -, -, -, -})%>


An ASPX view expression <%=%> can also be written in <%:%>

2. Master View
1. Create a master view
Master view, typically placed under views of a fixed folder, called "Shared", in this folder, you can
Right-click Add New Item in the shortcut menu, select the Open New Item window, select the MVC4 View master page
2. Special for master view and normal view
(1). The beginning of the page is the @master directive, which also has corresponding properties, including the language and Inherits properties, which default
The base class for inheritance is the System.Web.Mvc.ViewMasterPage class
(2). The page contains two <asp:ContentPlaceHolder/> tags, which are server-side tags that represent the contents of the page
Location of container and placement view
3. Using the template view
To create a content view that uses the template view, simply tick the "Use Layout or master page" check box and choose to create a good
Master view

(1) [email protected] Directive declaration more the MasterPageFile property is set to the path of the master view file
(2) .</asp:content> tag, corresponding to </asp:ContentPlaceHolder> tag in master view

ASP. NET MVC Overview and the first MVC program

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.