The 1th chapter enters the ASP. NET MVC World

Source: Internet
Author: User
Tags httpcontext

The 1th chapter enters ASP. NET MVC World ASP.

At present, the main technology to develop B/s system is ASP., JSP, PHP, etc. where ASP. NET is

Based on. NET platform is a server technology for creating dynamic Web pages that you can use to create dynamic interactions

Web page.

Figure 1.1. NET Framework and ASP.

Introduction to ASP. NET MVC

ASP. NET MVC is a subset of ASP., which is the perfect knot of ASP.

Similar to the Java Struts framework.

The first ASP. NET MVC program creates an ASP.

First, open the VS2012, execute the "file", "New", "Project" command, the left item

Type Select the "web" option in the middle of the. NET Framework version drop-down list, select. NET Frame

work4.5 "option, select" ASP. MVC4 Web Application ".

The project structure is described as follows:

>app_data folder: Used to store the database files contained in the system.

>app_start folder: A related class file containing ASP.

>Controllers folder: The controller code file where the entire project is placed.

>Models folder: A template code file that places the entire project.

> Views folder: A View code file that places the entire project.

Web . config file under > view: A configuration file that acts on the views.

>Global.asax Files: Global application files, typically used in conjunction with classes in the App_start folder.

>packages.confiG file: The version of the Assembly used to manage the project.

> Web. config file under the root directory: a configuration file that acts on the entire project.

Hello,asp.net MVC

1. Controller description

>system.web namespaces: ASP. Net Core namespace.

>SYSTEM.WEB.MVC Namespaces: The most basic namespace for developing ASP.

> Controller class: The base class that each controller must inherit.

>Index() method: The Controller's action method, which responds to the client request and invokes the corresponding view output information.

2. Basic rules

Controllers have been controller end, such as HomeController, when we create an action

Method Index (), then if you let this method return to the view, it will default to look under the View folder and control

The name of the folder home, in the search for file Plus and action method names of the index view.

How the ASP. NET MVC Program works

1 global.asax

In an ASP. Global.asax, the main thing is to do some global work when the system starts

For As can be seen from the above code, it calls the relevant classes in the App_start folder to complete these works

Called Routeconfig.register Routes () to complete the route registration;

Filterconfig.registerglobalfilters () to complete the global filter (similar to that in struts

Interceptor) Registration of the device.

2RouteConfig.cs file

The function of the RegisterRoutes () method here is to define a route, that is, to define a URL access rule.

3 view naming and addressing rules

4Web Server

There is a debug-related configuration in Web. config, as shown below.

<compilation debug= "true" targetframework= "4.5"/>

where debug= "true" means debugging is supported, and after a formal release, debug needs to be set to

False

Declaration and encoding of the ASPX view ASPX view

[email protected] instruction

/tr>

table 1-3 @Page directives Common Properties

Properties

Description

Language

Specify the language that the page code and the post code use, it is important to note that this is only supported in the Microsoft. NET Framework language

Inherits

Inherited page classes, aspx views generally inherit System.Web.ViewPage and

system.web.view<t> two types

ContentType

Specifies the MIME type and page response is the character encoding used

validaterequest

Specify whether to validate requests

MasterPageFile

Specify template view file for use

[email protected] instruction

In the ASPX view, you can introduce a new namespace through the @import directive.

<% @Import namespace= "Mvcdemo.helper"%>

3 server-side inline syntax

<%%> Small Script

<%=%> or <%:%> expressions

<script runat= "Server" ></script> server-side script block

Master view

1 Creating Master Views

<% @MasterLanguage = "C #" inherits= "System.Web.Mvc.ViewMasterPage"%>

<! Doctypehtml>

<metaname= "Viewport" content= "Width=device-width"/>

<title><asp:contentplaceholderid= "Titlecontent" runat= "Server"/></title>

<body>

<div>

<asp:contentplaceholderid= "MainContent" runat= "Server" >

</asp:ContentPlaceHolder>

</div>

</body>

2 working with master views

<% @PageTitle = "" Language= "C #" masterpagefile= "~/views/partialview/viewmasterpage1.master" inherits= " System.web.mvc.viewpage<dynamic> "%>

<asp:contentid= "Content1" contentplaceholderid= "titlecontent" runat= "Server" >

View1

</asp:Content>

<asp:contentid= "Content2" contentplaceholderid= "maincontent" runat= "Server" >

View1

</asp:Content>

3 resource File Load tool methods

Url.content ("~/css/style.css")//~ represents the server root directory

Asp. NET system objects Introduction to ASP.

Asp. The common system objects in net are shown in the following table

Table 1-4 ASP. NET System objects

Object Name

Description

Request

Used to get the values that the client sends during a Web request, such as URL parameters, form values, and so on.

Response

To be responsible for returning the HTTP output to the client

Application

A state object that acts on the entire program's run time, which can be used to hold configuration parameters for the entire application

Session

Session state Hold Object

Cookies

How the client maintains session information

Server

Tool objects for some server-side processing, such as URL encoding decoding, page forwarding

HttpContext

Encapsulates all HTTP-specific information about an individual HTTP request

1.Request Object

Members

Description

String ContentType

Gets or sets the MIME content type of the incoming request

HttpCookieCollection Cookies

Gets the collection of cookies sent by the client

Httpfilecollection Files

Gets the collection of files uploaded by the client

NameValueCollection Form

Get the data submitted by the form

NameValueCollection Headers

Get HTTP Header Collection

NameValueCollection QueryString

Get the HTTP query string variable collection

String RAWURL

Gets the original URL of the current request

NameValueCollection ServerVariables

Get a collection of Web server variables

String useragent

Get the original user agent information for the client browser

String userhostaddress

Get the IP host address of the remote client

String MapPath (String virtualpath)

The specified virtual path is mapped to a physical path

void SaveAs (String Filename,bool includeheaders)

To save an HTTP request to disk

2.Response

Members

Description

String ContentType

Gets or sets the HTTP MIME type of the output stream

HttpCookieCollection Cookies

Get Response Cookie Collection

NameValueCollection Headers

Gets the collection of response header

void Redirect (string url)

Redirect requests to a new URL

void Write (String s)

Writes a string to the HTTP response output stream

3.Server Object

Common Members

Description

String MapPath (String path)

Returns the physical file path corresponding to the specified virtual path on the Web server

void Transfer (String path)

Use the specified path for page forwarding

String UrlDecode (String s)

URL encoding of a string

String UrlEncode (String s)

URL encoding of a string

4.Session Object

Common Members

Description

String SessionID

Contains a unique user session identifier that can be used to record user information throughout the session.

int Timeout

The user time-out, in minutes, and the lifetime of the data in the session is limited to the time-out period.

void Abandon ()

End session, cancel current conversation

void Add (string name,object value)

Add Session Data

void Remove (string name)

Delete session data

5.Cookie Object

Common Members

Description

String Name

The name of the cookie object

String Value

Content (value) of the cookie object

DateTime Expires

The effective time of the cookie object. If the expiration date of the cookie is not set, they are only saved until the browser program is closed, or if the Expires property of the cookie object is set to

DateTime.MaxValue, it means that the cookie will never expire.

Grammar:

Response.cookies[cookie name].value= variable value;//write Cookie

The name of the String variable name =request.cookies[cookie]. value;//reading Cookies

Or

HttpCookie Hccookie = new HttpCookie ("Name of the cookie", "value");

RESPONSE.COOKIES.ADD (Hccookie);

6.Applicaion Object

Grammar:

appliation["Application name"]= value;//Stored value

Variable =application["Application name"];//value

7.HttpContext Object

Common Members

Description

HttpApplicationState Application

Application Object

HttpRequest Request

Request Object

HttpResponse Response

Response Object

HttpServerUtility Server

Server object

HttpSessionState Session

Session Object

IPrincipal User

User Object

System.Web.Caching.Cache Cache

Cache Object

Static HttpContext current

Gets or sets the current HTTP request

System.Web.HttpContext Object

In some special cases, it may be necessary to access these objects in other custom classes, which only need to be achieved by HttpContext's current property, as shown in the following code:

System.Web.HttpContext.Current.Response.Redirect ("~/");

The 1th chapter enters the ASP. NET MVC World

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.