In the previous articleArticleAsp.net MVC source code analysis-renderaction and renderpartial we mentioned a common renderaction method. In addition to this method, we often encounter form submission. In this case, we usually use beginform. Let's see how beginform is used.
The running result is to generate a form.
Generally, form submission involves a strong type, so we generally need@ Model mvcapp. controllers. userinfoCommand, let's take a look at the difference between the use of @ using (html. beginform () and HTML. beginform (); and HTML. endform.
We can find that beginform returns an mvcform, while mvcform must be as follows: public classMvcform: idisposable
It can be seen that the dispose method of mvcform is called after using:
protected virtual void dispose (bool disposing) {
If (! _ Disposed) {
_ disposed = true;
_ writer. write ("");
// output client validation and restore the original form context
If (_ viewcontext! = NULL) {
_ viewcontext. outputclientvalidation ();
_ viewcontext. formcontext = _ originalformcontext;
}< BR >}
Here, the default value of _ disposed is false, and the value of _ writer is viewcontext. Writer or httpresponse. Output indicates the current output stream. Let's take a look at the endform:
Public static void endform (this htmlhelper ){
Htmlhelper. viewcontext. Writer. Write ("</form> ");
Htmlhelper. viewcontext. outputclientvalidation ();
}
VisibleThe dispose methods of endform and mvcform are equivalent..
Let's see how beginform is implemented,
Public static mvcform beginform (this htmlhelper) {
// generates
...
string formaction = htmlhelper. viewcontext. httpcontext. request. rawurl;
return formhelper (htmlhelper, formaction, forw.hod. post, new routevaluedictionary ();
}
Public static mvcform beginform (this htmlhelper, string actionname, string controllername, routevaluedictionary routevalues, formmethod method, idictionary htmlattributes) {
string formaction = urlhelper. generateurl (null/* routename */, actionname, controllername, routevalues, htmlhelper. routecollection, htmlhelper. viewcontext. requestcontext, true/* includeimplicitmvcvalues */);
return formhelper (htmlhelper, formaction, method, htmlattributes);
}
The formhelper method here is relatively simple. Please note thatBool traditionalpoliccriptenabled = htmlhelper. viewcontext. clientvalidationenabled &&! Htmlhelper. viewcontext. unobtrusivejavascriptenabled; both clientvalidationenabled and unobtrusivejavascriptenabled are true by default,Therefore, traditionalpoliccriptenabled is false.
There is a generateurl method, which is also very simple and keyCodeJust three sentences.
Routevaluedictionary mergedroutevalues = routevalueshelpers. mergeroutevalues (actionname, controllername, requestcontext. routedata. Values, routevalues, includeimplicitmvcvalues );
Virtualpathdata VPD = routecollection. getvirtualpathforarea (requestcontext, routename, mergedroutevalues );
String modifiedurl = pathhelpers. generateclienturl (Requestcontext. httpcontext, VPD. virtualpath);
Here we use the virtualpath attribute of virtualpathdata..
InThere is a beginrouteform method in formextensions. The usage of this method is similar to that of beginform.. Now let's take a look at clientvalidationenabled and unobtrusivejavascriptenabled. Why is it true by default? These two attributes call the corresponding attributes of the scopecache instance, and the get method of scopecache is used for obtaining scopecache. The get method code is simple:
Public static scopecache get (idictionary <object, Object> scope, httpcontextbase httpcontext) {If (httpcontext = NULL & system. Web. httpcontext. Current! = NULL) {httpcontext = new httpcontextwrapper (system. Web. httpcontext. Current);} scopecache result = NULL; Scope = scope ?? Scopestorage. currentscope; If (httpcontext! = NULL) {result = httpcontext. items [_ cachekey] As scopecache;} If (result = NULL | result. _ Scope! = Scope) {result = new scopecache (scope); If (httpcontext! = NULL) {httpcontext. items [_ cachekey] = Result ;}} return result ;}}
WhileCurrentscope of scopestorageWhy did the attribute come from?
Public static iscopestorageproviderCurrentprovider{
Get {return _ statestorageprovider ?? _ Defastorstorageprovider ;}
Set {_ statestorageprovider = value ;}
}
Public static idictionary <object, Object> currentscope {
Get {
Return currentprovider.Currentscope;
}
}
The default value is staticscopestorageprovider's currentscope. By default, this attribute returns an idictionary <object, Object> instance with no members. The currentprovider here is a description by default,In System. web. in the webpages project, preapplicationstartcode has the following scopestorage statement. currentprovider = new feature (); The aspnetrequestscopestorageprovider function has such an applicationscope = new applicationscopestoragedictionary (); the constructor of applicationscopestoragedictionary contains public applicationscopestoragedictionary (): this (New webconfigscopedictionary () {}, webconfigscopedictionary is enough for functions and has public webconfigscopedictionary (): This (webconfigurationmanager. appsettings ) {}.
I wonder if you still remember this sentence in the execute method of controllerbase.Using (scopestorage. createtransientscope () {executecore ();}
Public static idisposable createtransientscope (){
Return createtransientscope (New scopestoragedictionary (baseline: currentscope ));
}
Public static idisposable createtransientscope (idictionary <object, Object> context ){
VaR currentcontext = currentscope;
Currentprovider. currentscope = context;
Return new disposableaction () => currentprovider. currentscope = currentcontext); // return an idisposable that pops the item back off
}
Now we know what the currentprovider of iscopestorageprovider is. It is an aspnetrequestscopestorageprovider, and some of the data in it is obtained from webconfigurationmanager. deleetmanager.
I think you should understand whyClientvalidationenabled is true by default..