ASP.NET partial intelligence points

zhaozj2021-02-16  94

When the alpha version, MS called it ASP , and later came out, it was called ASP.NET, what is it? This is not just a simple question, but what is the problem it is. What is ASP.NET is, I am afraid you have seen a lot of such articles, and have your own understanding, but I am afraid that most people's understanding is not so correct. Ask a simple question, what is the relationship between ASP.NET and ASP? I am afraid the view of Chinaasp .NET version of the moderator represents most people's understanding: ASP is more than ASP :), really like this? Let us look at the following example, this code is the submission button of my own ASP.NET BBS user registration module, and the function is to store the data submitted into the database.

/ / Submit button Click

Public void OnSubmit (Object Sender, Eventargs E)

{

IF (page.issalid)

{

// Data Introduction

Try

{

BBSUSER myUser = new bbsuser ();

IF (! myuser.getuser (txtusername.text))

{

Myuser.createuser (bbsuser.createType.create, txtusername.text, txtpassword.text,

TXTEMAIL.TEXT, TXTHOMEPAGE.TEXT, "" "

}

}

Catch (Exception Exp)

{

#if Debug

Response.write ("Exception:" Exp.Message);

Return;

# Endif // debug

Server.Transfer ("Error.aspx");

}

}

}

How is it, is it some difference between ASP you used to use? You can not see the data store in the code, and all encapsulate to the CreateUser method of the BBSUser class, maybe you will say ASP, do you have a function or process? Yes, it is true that ASP.NET can also do it directly into a process and then call it, but if so, it is really only more than ASP. Let us start from the head.

The biggest difference between ASP.NET and ASP is the conversion of programming thinking, not just the enhancement of functionality. ASP uses the scripting language such as VBS / JS to program, and those scripting languages ​​belong to weak types, facing structural programming languages, rather than domain, which significantly produces the following issues:

1, code logic confusion, difficult to manage: Since the ASP is a script language mixed HTML programming, you can't see the logical relationship of the code, and as the complexity of the program increases, it makes the management of the code difficult, and even exceeds a programmer office. Can achieve management capabilities, resulting in errors or such problems.

2, the reusability of the code: due to the structure-oriented programming method, and mixed HTML, it is possible to modify a point, and the entire program needs to be modified, and the code is reused.

3, the weak type causes potential error: Although the programming language of weak data types uses back to convenient, it is far from being lost relative to what it caused.

The above is the weakness of the language itself. There is also a problem in the functional ASP. The first is that the function is too weak. Some underlying operations can only be completed by components. At this point, it is far more than PHP / JSP, followed by lack of improvement. The error correction / debug function, this ASP / PHP / JSP is similar to this. So, what improvements are ASP.NET? ASP.NET is getting rid of the shortcomings of previous ASPs using scripting languages. It can use any programming languages ​​including C , VB, JS, etc., of course, the most appropriate programming language or MS is a C # (read) Csharp), it can be seen as a mixture of VC and Java, although MS yourself speaks more like VC in the C # core, but in fact, I still think that it is more like it and Java. First it is an object-oriented programming language, not a script, so it has all characteristics of object-oriented programming languages, such as encapsulation, inheritance, polymorphism, etc., which solves the ASPs just mentioned. weakness. The encapsulation makes the code logic clear, easy to manage, and apply to the ASP.NET to separate the business logic and HTML pages, so regardless of how the page prototype changes, business logic code does not have to do any changes; inheritance and polymorphism The reusability of the code is greatly improved, you can maximize your previous investment by inheriting existing objects. And C # and C , Java provide a complete debug / error correction system. Ok, one breath has said such a one of the theoretical things, it is better to specifically explain it in conjunction with an example in the future, I will specifically describe the development of ASP.NET in conjunction with an example of a forum.

Yes, you must explain that this tutorial is not an ASP.NET entry tutorial. I assume that you have a certain understanding of ASP.NET, C # and object-oriented programming. If this is not the case, please read the article or tutorial first.

The biggest difference between ASP.NET and ASP is the conversion of programming thinking, then let's take a look at how to convert programming ideas. The previous Web programming process from CGI (Perl) to ASP, PHP, and JSP programming process: the artist gives the page prototype, the programmer is filled with the page, and finally, the next time if the prototype changes, then Modify the program so that the business logic is mixed with the HTML page, it can be said that it is half a time. So, now there is ASP.NET, what should we do?

Let us find an actual example, take the forum, first look at its business logic from top to bottom. We can regard a forum to make an object, it has its own properties and methods, the common attributes have name, number of posts, number of users, layout, etc. In this way, we can construct the forum object:

Namespace MyownClass

{

Using system;

Using system.data.sql;

Using system.data;

//

// Class Name: BBS

//

// DESCRIPTION: Forum class, construct a forum object

//

// Date: 2000/02/03

//

///

Public Class BBS

{

// Private variable

Private string m_strtitle; // bbs name

Private int m_intforumcount; // layout

Private int m_inttopiccount; // Post number

Private int m_intusercount; // Registered users

//Attributes

Public String Title {

get

{

Return M_STRTILE;

}

}

Public int forumcount

{

get

{

Return M_INTFORUMCOUNT;

}

}

Public int topiccount

{

get

{

Return M_INTTTOPICCOUNT;

}

}

Public int Usercount

{

get

{

Return M_IntuserCount;

}

}

//Constructor

Public BBS (String a_STRTITLE)

{

//

// Todo: add constructor logic here

//

m_strtitle = a_strtitle;

// Read the database

MyConnection myconn = new myconnection ();

Sqlcommand mycommand = new sqlcommand ();

MyCommand.activeConnection = myconn;

Mycommand.commandtext = "UP_GETBBSINFO"; // Call the stored procedure

Mycommand.commandtype = commandtype.storedProcedure;

Try

{

Myconn.open ();

SqlDataReader MyReader;

MyCommand.execute (Out MyReader);

IF (MyReader.Read ())

{

m_intforumcount = (int) MyReader <"forumcount">;

m_inttopiccount = (int) MyReader <"Topiccount">;

m_intusercount = (int) MyReader <"Usercount">;

}

Else

{

Throw (New Exception ")));

}

//Clearance

MyReader.Close ();

MyConn.close ();

}

Catch (SQLException E)

{

Throw (New Exception);

}

}

}

}

This BBS class is very simple, there are four private variables, corresponding to four read-only properties, and only one constructor with parameters is to read the corresponding data from the database, populate four private variables. Class construction, let us see how to use, in the page file (.aspx) that needs to display the forum (.aspx), construct four Label, like this:

Number of registered users:

Total number of posts:

Lotion:

Then use this in the corresponding C # file:

Protected Void Page_init (Object Sender, Eventargs E)

{

//

// Codegen: This Call is Required by The ASP Windows Form Designer.

//

InitializationComponent ();

// Initialization page object

// Create a BBS object

Try

{

m_objbbs = new bbs ("Yingxiangshan Zhuang Forum");

}

Catch (Exception Exp)

{

#if Debug

Response.write ("Initialization BBS object error:" Exp.Message "");

Return;

# Endif // debug

Server.Transfer ("Error.aspx");

}

// Forum name

LBLBBSNAME.FORECOLOR = color.white;

LBLBSNAME.TEXT = m_objbbs.title;

//User number

LBluserCount.Forecolor = color.white;

LBluserCount.Text = m_objbbs.usercount.tostring ();

// article number

LBltopiccount.ForeColor = color.white;

Lbltopiccount.text = m_objbbs.topiccount.tostring ();

// layout

LBLFORUMCOUNT.FORECOLOR = color.white;

LBLFORUMCOUNT.TEXT = m_objbbs.forumcount.toString ();

}

See the benefits of such use? Yes, the business logic and HTML code are separated, so no matter how the page prototypes modify, the code does not need to do it. The BBS object is constructed, let's take a look at other objects of the forum, they are the user (BBSUSER), a layup, and a Topic, I will explain in detail in the next content.

Let me talk about how to construct a BBS object, there are friends asking me to introduce how to construct the object in C #, let's simply say it, it is enough to make up this lesson.

Class C #, can also be called objects (Object), which consists of the following parts: member variables, attributes, and methods, which are essential that this class does not have a constructor for any parameters, it does not specify back Type, the role is the member variable of the initialization class, allocated memory, etc. Unlike C , C # classes only constructor, do not need the pavilion function, that is, you only need to assign memory for member variables, not the explicit release of memory, because C # and Java are all passed through the garbage collector To release memory. Understand these, we can construct a simple class, one thing that does not do:

Public Class Myclass

{

Public myclass () {};

}

It only needs a simple New one when you want to use, like this: myclass myclass1 = new myclass (); don't doubt, you have created an object, although it doesn't do anything :). Below we add a private character-type member variable m_strtitle, and initialize this member variable in the constructor, the entire class definition becomes like this:

Public Class Myclass

{

// Private member variable

PRIVATE STRING M_STRTITLE;

//Constructor

Public myclass ()

{

m_strtitle = "I have already been assigned"; "

}

}

CAUTION Define this line code for member variables: private string m_strtitle; where String is well understood, explain that m_strtitle is a string type, then what is the most in front of the private? This private indicates that this member variable is private, only the functions of this class can be used, and any otherwhere includes a function of subclasses, in addition to Private, you can also define into public (public) and protected (Protection), public indicates that this member variable can be used anywhere, and protected indicates that this variable can only be used in this class or subclass. So, if we want to use this member variable, you can define it into public, but for object-oriented programming, this is not a good program habit, because it destroys the encapsulation of the class, the good method in C is Define public functions to access this private variable, and C # provides a more convenient way, which is property (Property), now we add this property title plus: Public Class Myclass

{

// Private member variable

PRIVATE STRING M_STRTITLE;

//Attributes

Public String Title

{

get

{

Return this.m_strtitle;

}

set

{

THIS.M_STRTILE = VALUE;

}

}

//Constructor

Public myclass ()

{

m_strtitle = "I have already been assigned"; "

}

}

Let's take a look at how to define attributes, first we need a scope qualifier, usually we use public, indicating that you can use this property anywhere, followed by two keywords Need Note: this and value, this represents the class itself, so THIS.M_STRTITLE is a member variable M_STRTITLE that represents this class, Value represents the value of the right side of the left value as the left value, like this: myclass.title = "Hello", the value of Value is "Hello", ok, This class is already available, like below:

Public static void main (string [] args)

{

Myclass myclass = new myclass (); // Construct an instance of the MyClass class

Console.writeline (MyClass.title); // The result is: I have been assigned.

Myclass.title = "My value changed"; // change the value of the Title property

Console.writeline (MyClass.title); // At this time, the result is changed.

}

Ok, let us add a MyMethod method to this class, this method does not return a value, with a character type parameter.

Public Class Myclass

{

// Private member variable

PRIVATE STRING M_STRTITLE;

//Attributes

Public String Title

{

get

{

Return this.m_strtitle;

}

set

{

THIS.M_STRTILE = VALUE;

}

}

//Constructor

Public myclass ()

{

m_strtitle = "I have already been assigned"; "

}

//method

Public void mymethod (String a_str) {

THIS.M_STRTILE = A_STR;

}

}

This is what we can rewrite the program just now, and the results are just just the same:

Public static void main (string [] args)

{

Myclass myclass = new myclass (); // Construct an instance of the MyClass class

Console.writeline (MyClass.title); // The result is: I have been assigned.

Myclass.mymethod ("My value changed"); // change the value of the Title property

Console.writeline (MyClass.title); // At this time, the result is changed.

}

The above briefly tells how to define classes. After reading these content, you can understand the BBS object we constructed in the previous section, let's take another definition:

Namespace MyownClass

{

Using system;

Using system.data.sql;

Using system.data;

//

// Class Name: BBS

//

// DESCRIPTION: Forum class, construct a forum object

//

// Date: 2000/02/03

//

///

Public Class BBS

{

// Private variable

Private string m_strtitle; // bbs name

Private int m_intforumcount; // layout

Private int m_inttopiccount; // Post number

Private int m_intusercount; // Registered users

//Attributes

Public String Title

{

get

{

Return M_STRTILE;

}

}

Public int forumcount

{

get

{

Return M_INTFORUMCOUNT;

}

}

Public int topiccount

{

get

{

Return M_INTTTOPICCOUNT;

}

}

Public int Usercount

{

get

{

Return M_IntuserCount;

}

}

//Constructor

Public BBS (String a_STRTITLE)

{

//

// Todo: add constructor logic here

//

m_strtitle = a_strtitle;

// Read the database

MyConnection myconn = new myconnection ();

Sqlcommand mycommand = new sqlcommand ();

MyCommand.activeConnection = myconn;

Mycommand.commandtext = "UP_GETBBSINFO"; // Call the stored procedure

Mycommand.commandtype = commandtype.storedProcedure;

Try

{

Myconn.open ();

SqlDataReader MyReader;

MyCommand.execute (Out MyReader);

IF (MyReader.Read ())

{

m_intforumcount = (int) MyReader <"forumcount">;

m_inttopiccount = (int) MyReader <"Topiccount">; m_intusercount = (int) MyReader <"Usercount">;

}

Else

{

Throw (New Exception ")));

}

//Clearance

MyReader.Close ();

MyConn.close ();

}

Catch (SQLException E)

{

Throw (New Exception);

}

}

}

}

And the slightly different from our threy, first look at the first line of Namespace Myownclass, the name space of this class is MyownClass, the namespace is like a package, which can contain many classes. Look at this line: use system; this tells the compiler, I want to reference the object in the SYSTEM name space. Then other other understand it?

转载请注明原文地址:https://www.9cbs.com/read-12187.html

New Post(0)