New Features of ASP. net4.0

Source: Internet
Author: User

I have noticed some changes in Web development support when I tried vs2010 in the past. For this reason, I also made a special PPT, which was originally intended to be discussed on April 10, April 12, as a result, this lecture on livemeeting was abandoned due to lack of voice for some inexplicable reasons.

Added a project template.
 
When creating a web project, you can see that more web project templates are added.
The corresponding situation in vs2008 is as follows:
 
The new template has the following improvements:
Basic membership functions. Authentication is required for most websites and applications. Therefore, the authentication function is added to the new template so that users can quickly get started with identity authentication in the web project squadron, the files of the authentication module are stored in the account folder;
The default master page is added. You can use the master page to easily display Website webpages. For example, if the top page, user logon Status display, and menu are involved, the default master page file name is site. master;
Added the default CSS style file. CSS sample files allow us to easily adjust the website style. A style file named site.css will be added when a website is created, which is located in the styles folder;
Mini web. config file. In vs2010, the Web. config file is very small, and most of the configurations are stored in the machine. config file;
Integrate jquery. Jquery is a very powerful JavaScript class library, which allows web developers to conveniently operate XHTML documents. It is supported only when SP1 is installed in vs2008, and directly supported in vs2010, the jquery class library file is in the scripts folder.
Through the above practice, the composition structure of our website is similar, which makes it easier to maintain (unlike earlier versions, the folders for storing such files are different ).

Better SMART awareness of JavaScript and HTML code
In earlier versions of Vs, the support for JavaScript intelligent sensing is relatively poor, and there is no support for HTML code segments. However, this function is now available in vs2010.
 
The intelligent perception of JavaScript scripts is improved in vs2010 to detect the function names and variables on this page.
 
Support for HTML code segments in vs2010

Enhanced control over the Server Control ID
We know that an ASP. NET Server Control has three IDs: ID, clientid, and uniqueid.
The ID indicates the server ID of the server control. In the server code, the ID uniquely identifies the server control. Therefore, the ID must be unique on the same page );
Clientid is the Client ID after the server control is converted to an HTML element. To prevent name conflicts, ASP. NET automatically generates a unique clientid value for each server control on the page. The clientid value is generated by connecting the control's ID value and its parent control's uniqueid value. Each part of the generated ID is separated by an underscore;
Uniqueid is the unique identifier of the server control that is defined in hierarchical form. It is also the name identifier of the client after the server control is converted into an HTML element.
Sometimes we need to use JavaScript on the client to operate HTML elements through the HTML element ID. If the server control is in the data binding control, the ID of the final HTML element is very different from what we usually use, which makes it inconvenient for us to perform operations, especially when we use JavaScript to implement full data selection and invert selection, when operating a single control, we can also write JavaScript Code as follows:
<SCRIPT type = "text/JavaScript"> <! -- <Br/> function dosomething () {<br/> alert ('<% = control. clientid %> '); <br/>}< br/> // --> </SCRIPT>
ASP. NET provides stronger control over the ID of the HTML element corresponding to the server control, and adds the clientidmode attribute for control. The following is an example of the clientidmode attribute usage. The server encoding is as follows:

<Asp: gridview id = "gvuserlist" runat = "server" autogeneratecolumns = "false"> <br/> <columns> <br/> <asp: templatefield headertext = "userid_autoid"> <br/> <itemtemplate> <br/> <asp: label runat = "server" id = "lB1" clientidmode = "autoid" text = '<% # eval ("userid ") %> '/> <br/> </itemtemplate> <br/> </ASP: templatefield> <br/> <asp: templatefield headertext = "userid_static"> <br/> <itemtemplate> <br/> <asp: label runat = "server" id = "lB1" clientidmode = "static" text = '<% # eval ("userid ") %> '/> <br/> </itemtemplate> <br/> </ASP: templatefield> <br/> <asp: templatefield headertext = "userid_predictable"> <br/> <itemtemplate> <br/> <asp: label runat = "server" id = "lB1" clientidmode = "predictable" text = '<% # eval ("userid ") %> '/> <br/> </itemtemplate> <br/> </ASP: templatefield> <br/> </columns> <br/> </ASP: gridview>
If we bind the data before viewing the generated HTML source code, we will see a similar part:
<Table cellspacing = "0" rules = "all" border = "1" id = "maincontent_gvuserlist" style = "border-collapse: collapse;" mce_style = "border-collapse: collapse; "> <br/> <tr> <br/> <TH scope =" col "> userid_autoid </Th> <TH scope =" col "> userid_static </Th> <th scope = "col"> userid_predictable </Th> <br/> </tr> <br/> <TD> <span id = "ctl00_maincontent_gvuserlist_ctl02_lb1 "> 136 </span> </TD> <br/> <TD> <span id =" lB1 "> 136 </span> </TD> <br/> <TD> <span id = "maincontent_gvuserlist_lb1_0"> 136 </span> </TD> <br/> </tr> <br/> .... <br/> </table>
We can see the Client ID of the controller when clientidmode = "autoid" and our early ASP. net. The client ID of the controller does not change when clientidmode = "static" is used. The client ID of the controller also carries a data row ID in clientidmode = "predictable" (for example: id = "0" in "maincontent_gvuserlist_lb1_0" indicates that the position in the data source is 0, that is, the first record ).
By specifying the clientidmode attribute, we can easily control the ID of the HTML element generated by the server-side control, which is also quite convenient.

Viewstate View Control
In the previous development process, viewstate was a fascinating and hateful thing: good use can bring great convenience, and poor use can also bring great trouble. We know that viewstate is stored in the form of hidden domains on the client. For the sake of confidentiality, the viewstate is base64-encoded and saved in the hidden domain on the client, after the data is submitted to the server, the server decodes the hidden data in the webpage using base64 to extract the control data stored in the hidden domain. Because HTTP requests are stateless and non-connected, viewstate can be used to store data between different requests on the same page, which is indeed convenient; however, if the status data of the control is large, the viewstate becomes larger and the response speed is affected. In ASP. in versions earlier than net4.0, viewstate is enabled by default. To reduce the adverse effect of viewstate, we have to set whether viewstate is enabled on the page or in the control (although we can do this on the web. in config, viewstate is disabled globally, but this attribute may be used indirectly in some cases. For example, in the management background, because we have fewer operations, we do not pay much attention to performance ).
Now in ASP. in net 4.0, all of this has been changed. In ASP. net4.0 does not enable the viewstate attribute by default, but you can enable it for a widget independently (although you may disable it at a higher level ).
See the following code:

<Asp: Panel runat = "server" id = "Panel" viewstatemode = "disabled"> <br/> <asp: label runat = "server" id = "lbenableviewstate" viewstatemode = "enabled" text = "set in code"/> <br/> <asp: label runat = "server" id = "lbdisableviewstate" text = "set in code"/> <br/> </ASP: Panel> <br/> <asp: button id = "button1" runat = "server" text = "btntestviewstate"/>
The background code is as follows:

If (! Page. ispostback) <br/>{</P> <p> lbdisableviewstate. TEXT = "The viewstate ID enabled"; <br/> lbenableviewstate. TEXT = "The viewstate ID enabled"; <br/>}
The first running effect of the page is as follows:
 
Because it meets the requirements during the first running! Page. ispostback judgment, so the text attribute is set to "The viewstate ID enabled". When you click the page button, it becomes the following effect:
 
The reason for this result is that the text of the first label is saved through viewstate, and the second label control is disabled. Therefore, status data is not saved after the page is refreshed.

Seo optimization support
For foreign websites, the search engine will read the description and keywords fields in the webpage, so that the search engine can identify our webpage through these two meta nodes, it is much easier to know the content of a webpage than simply analyzing the frequency of words on a webpage, therefore, if we make good use of these two things to search for the same content, our web page may display a relatively high level. This is the so-called SEO (search engine optimization, that is, search engine optimization) technology, in the previous ASP. net, but in ASP. in net4.0, we can directly operate in the code.
The following code:

If (! Page. ispostback) <br/>{< br/> page. title = "here you can set the title of the page"; <br/> page. metadeworkflow = "here you can set the description of the page"; <br/> page. metakeywords = "here you can set keywords on the page for Seo"; <br/>}
In this way, we can see the following in the HTML source code of the final webpage:
<Meta name = "Description" content = "here you can set the description of the page"/> <br/> <meta name = "keywords" content = "here you can set keywords, SEO "/>
In this way, it is much easier to optimize the search engine.
Note two points: 1. Seo involves a lot of things and complicated operations. Setting Description and keywords is just one aspect. 2. Due to the relentless pursuit of Seo by Chinese people, many search engines are not too cool at these two fields on Chinese websites.
Routing
Those who have done MVC may be familiar with routing functions, especially those who have ASP. net mvc. We know that the usual form of blog or store URL with the username zhoufoxcn may be as follows:
Http://www.xxx.com/user.aspx? Username = zhoufoxcn
Http://www.xxx.com/user.aspx? Userid = 4419544 (if the userid corresponding to zhoufoxcn is 4419544)
This is very difficult to remember. If we adopt URL rewriting, we can become more friendly, such:
Http://www.xxx.com/user/zhoufoxcn.aspx (note depends on URL Rewrite Rules)
If a route is used, the URL can be more user-friendly and easier to search for, for example:
Http://www.xxx.com/user/zhoufoxcn
To use a route in a non-ASP. net mvc project, follow these steps:
Configure the route when the ASP. NET application is started. When the ASP. NET application is started, the code in application_start method in global. asax. CS will be executed. we can write it like this:

Void application_start (Object sender, eventargs e) <br/>{< br/> // routetable. routes. ignore (); <br/> // code that runs on application startup <br/> # region route setting <br/> // parameter description: <br/> // The route name is "userlist" and the routing rule is "{userlist }","~ /Default. aspx "is the processing page <br/> routetable. routes. Add (" userlist ", new route (" {userlist}/{page} ", new pageroutehandler ("~ /Userlist2.aspx "); <br/># endregion <br/>}
Then, write as follows:

Link. TEXT = "|" + I. tostring () + "|"; // the link is the hyperlink control in the night <br/> string expression = string. format ("routename = {0}, userlist = {1}, page = {2}", "userlist", "userlist", I); <br/> link. navigateurl = routeurlexpressionbuilder. getrouteurl (this, expression );
This completes the use of the routing function.

Summary:
In addition to the above mentioned, some new server controls, such as queryextender, are added to ASP. net4.0. Using ASP. net4.0, we can develop ASP. NET application control more flexibly, which makes it easier to meet the needs of different occasions.

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.