Before getting started, let's take a look at the usage of the Params Keyword:
Params
ParamsYou can specify the parameter when the number of parameters is variable.
- In the method declarationParamsNo other parameters are allowed after the keyword, and only one parameter is allowed in the method declaration.ParamsKeywords
First, customize the view
1 Public Class Debugview: iview 2 { 3 Public Void Render (viewcontext, system. Io. textwriter writer) 4 { 5 Write (writer, " ----- Routedata ------ " ); 6 Foreach ( String Key In Viewcontext. routedata. Values. Keys) 7 { 8 Write (writer," Key: {0} value: {1} " , Key, viewcontext. routedata. Values [Key]); 9 } 10 11 Write (writer, " -------- View data -------- " ) 12 Foreach ( String KeyIn Viewcontext. viewdata. Keys) 13 { 14 Write (writer, " Key: {0}, value: {1} " , Key, viewcontext. viewdata [Key]); 15 } 16 } 17 18 Private Void Write (textwriter writer, String Template, Params Object [] Values) 19 { 20 Writer. Write ( String . Format (template, values) + " <P/> " ); 21 } 22 }
Then, create a view Engine
1 Public Class Debugviewengine: iviewengine 2 { 3 Public Viewengineresult findpartialview (controllercontext, String Partialviewname, Bool Usecache) 4 { 5 Return New Viewengineresult ( New String [] { " Debug data view Engine " }); 6 } 7 8 Public Viewengineresult findview (controllercontext, String Viewname,String Mastername, Bool Usecache) 9 { 10 If (Viewname = " Debugdata " ) 11 { 12 Return New Viewengineresult (New Debugview (), This ); 13 } 14 Else 15 { 16 Return New Viewengineresult ( New String [] { " Debug data view Engine " }); 17 } 18 } 19 20 Public Void Releaseview (controllercontext, iview view) 21 { 22 Throw New Notimplementedexception (); 23 } 24 }
Finally, register the view engine:
1 Protected VoidApplication_start (){2 Arearegistration. registerallareas ();3Viewengines. Engines. Add (NewDebugdataviewengine ());4 Registerglobalfilters (globalfilters. filters );5 Registerroutes (routetable. routes );6}
Here, the same applicationProgramMultiple View engines are supported. If multiple view engines support the same view, only the first view engine is displayed in the set order. Therefore, if you want to prioritize a view engine, the best way is to place it in the first place.
Viewengines. Engines. insert (0, new debugdataviewengine ());
Next we can perform the test:
Create a homecontroller and write the followingCode:
1 Public Class Homecontroller: Controller 2 { 3 // 4 // Get:/home/ 5 6 Public Viewresult index () 7 { 8 Viewdata [ " Message " ] = " Hello New View Engine " ; 9 Viewdata [ " Time " ] = Datetime. Now. tow.datestring (); 10 Return View ( " Debugdata " ); 11 } 12 13 }
Run to view the running results:
Here, we have created a custom view. The next article will explain how to use the built-in razor view.