Update: this is a wrong idea. For details, please refer to the fact that the Ajax world requires ASP. net mvc.
ASP. net mvc has not yet been used in the development of the blog park, and it seems that it cannot keep up with the times. But whether a technology is used is critical to whether the technology can actually solve practical problems.
This articleArticleThe following uses the short message feature of the new version of the blog park we are developing as an example to describe why ASP. net mvc is not needed.
Function introduction:Short Message inbox, which displays the list of short messages of the current user.
The main technologies used:Master page, jquery, jquery plugin templates, and WCF.
Templates is a jquery plug-in developed by Microsoft and features similar to ASP. NET repeater controls. templates will be integrated into jquery 1.5 to become the core component. For more information about templates, read Microsoft and jquery.
MainCode:
1. ASP. NET page file:
<% @ Page title = "" Language = " C # " Masterpagefile = " MSG. Master " %>
< ASP: Content ID = "Contentmain" Contentplaceholderid = "Cphmain" Runat = "Server" >
< Div ID = "Msg_list" >
</ Div >
< Script ID = "Contenttmpl" Type = "Text/X-jquery-tmpl" >
< Div class = " Msg_item " >
< Div class = " Msg_sender " > $ {Sendername} < / Div>
< Div class = " Msg_title " > $ {Subject} < / Div>
< Div class = " Msg_sendtime " > $ {Sendtime} < / Div>
< / Div>
</ Script >
< Script Type = "Text/JavaScript" >
Getmsglist ( 1 , 30 );
</ Script >
</ ASP: Content >
The above Code uses the getmsglist method A to call WCF to obtain the short message list and display it in msg_list.
Here, the master page is used to reuse common HTML elements. Otherwise, only one HTML file is enough and. aspx is not required. Why ASP. net mvc? For such an Ajax application, you only need to load a simple HTML page and then hand it over to JS. MVC is required, which is also carried out in JS. ASP. net mvc is useless.
No ASP. net mvc is required.
To fully introduce this example, continue with our code.
2. js code:
A) JS file reference
< Script Type = "Text/JavaScript" SRC = "Http://common.cnblogs.com/script/jquery.js" > </ Script >
< Script Type = "Text/JavaScript" SRC = "Http://common.cnblogs.com/script/json2.js" > </ Script >
< Script Type = "Text/JavaScript" SRC = "JS/jquery. tmpl. Min. js" > </ Script >
< Script Type = "Text/JavaScript" SRC = "JS/msg. js" > </ Script >
Json2.js is used to convert JS objects to JSON.
Jquery. tmpl. Min. JS is a reference file of the jquery plugin templates.
The getmsglist method is defined in MSG. js.
B) getmsglist method definition:
Function Getmsglist (pageindex, pagesize ){
VaR Msgquery = {}
Msgquery. pageindex = Pageindex;
Msgquery. pagesize = Pagesize;
$. Ajaxsettings. Type = ' Post ' ;
$. Ajaxsettings. datatype = ' JSON ' ;
$. Ajaxsettings. contenttype = ' Application/JSON ' ;
$. Ajaxsettings. url = ' /Services/ajaxmsgservice. svc/getmsglist ' ;
$. Ajaxsettings. Data = ' {"Msgquery ": ' + JSON. stringify (msgquery) + ' } ' ;
$. Ajaxsettings. Success = Function (Data ){
$ ( " # Contenttmpl " ). Tmpl (data. d). appendto ( " # Msg_list " );
};
$. Ajax ();
}
"/Services/ajaxmsgservice. SVC" -- The called WCF Service. For details about how jquery calls WCF, refer to discard WebService and use jquery to call WCF in. net4.
"JSON. stringify (msgquery) "-- converts a msgquery object to JSON. The method in WCF is" getmsglist (sitemsgquery msgquery )", the result is that the JS object is passed as a parameter to the WCF method. In my previous comments, some people mentioned that jquery calling. ashx is more convenient. From the effect here, I think jquery calling WCF (or. asmx) is more convenient.
$ ("# Contenttmpl "). tmpl (data. d ). appendto ("# msg_list"); -- bind the data returned by WCF to contenttmpl (see Code 1) to form a list and load it to msg_list to display the data. This is the role of the templates plug-in.
3. WCF code:
[Servicecontract]
[Aspnetcompatibilityrequirements (requirementsmode =
Aspnetcompatibilityrequirementsmode. Allowed)]
Public Class Ajaxmsgservice
{
[Operationcontract]
Public Sitemsg [] getmsglist (sitemsgquery msgquery)
{
Int Spaceuserid = Util. getcurrentuser (httpcontext. Current). spaceuserid;
Msgquery. sitemsg = New Sitemsg () {recipientspaceuserid = Spaceuserid };
// Call the back-end WCF Service
Msgserviceclient Client = New Msgserviceclient ();
Sitemsg [] sitemsglist = Client. getmsglist (msgquery );
Try {Client. Close ();}
Catch {Client. Abort ();}
Return Sitemsglist;
}
}
Here, the WCF is equivalent to a transfer station. It calls the WCF Service on another server in the background, obtains the short message data, and returns it to the client Js in JSON format.
The example has been introduced. You are welcome to point out the shortcomings.
Summary
With more and more web applications using Ajax, ASP. net mvc has a smaller and smaller space to use its skills.
Currently, the only obstacle to AJAX is that the search engine cannot index the content loaded by Ajax, but this situation will certainly change. Ajax is the trend and web app is the trend.