1.IView interface
2.IViewEngine interface
Viewengineresult
3. Registering the View engine
// clear the View engine ViewEngines.Engines.Clear (); // Add a view engine VIEWENGINES.ENGINES.ADD (new debugdataviewengine ()); // or Add View engine (order) ViewEngines.Engines.Insert (0,new debugdataviewengine ());
The MVC framework support for view engines is implemented by the Controlleractioninvoker class and is built into the Iactioninvoker interface. When
When you implement your own action Invoker or controller factory directly via the Iactioninvoker or Icontrollerfactory interface, you will not be able to access the view engine features automatically.
First, using the Razor view engine
The Razor view engine compiles the view of the application to improve performance. The view is converted to a C # class and then compiled, which is why it is convenient to include C # snippets in the view.
1. Configure the View search location
Razor View engine Search properties
Property |
Describe |
Default value |
Viewlocationformats Masterlocationformats Partialviewlocationformats |
Find views, partial views, and location of layouts |
"~/views/{1}/{0}.cshtml", "~/views/{1}/{0}.vbhtml", "~/views/shared/{0}.cshtml", "~/views/shared/{0}.vbhtml" |
Areaviewlocationformats Areamasterlocationformats Areapartialviewlocationformats |
Find a view, a partial view, and the location of a layout for a region |
"~/areas/{2}/views/{1}/{0}.cshtml", "~/areas/{2}/views/{1}/{0}.vbhtml", "~/areas/{2}/views/shared/{0}.cshtml", "~ /areas/{2}/views/shared/{0}.vbhtml " |
The parameter value corresponding to the dot character:
{0} represents the view name
{1} represents the controller name
{2} represents a zone domain name
Create a derived class of the Razorviewengine class and modify the property values to change the search location. (To re-register the View engine)
Public class Customlocationviewengine:razorviewengine { public customlocationviewengine () { newstring[] { "~/views/{1}/{0}.cshtml", " ~/views/common/{0}.cshtml " }; } }
protected void Application_Start () { arearegistration.registerallareas (); ViewEngines.Engines.Clear (); VIEWENGINES.ENGINES.ADD (new customlocationviewengine ()); Webapiconfig.register (globalconfiguration.configuration); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (bundletable.bundles); }
2. Add Dynamic content
How to add dynamic content
Technology |
When to use |
Inline code |
Used for small, self-contained view-logic fragments, such as an if or foreach statement. This is the basic figure for creating dynamic content, and it is also the basis for some other methods. |
HTML helper Methods |
Used to generate a separate HTML element or collection of small fragments, typically based on the values of the view model or view data. MVC contains a number of useful HTML helper methods, and it's easy to create your own helper method methods. |
Segmentation |
Used to create content segmentation, which is used to insert to a specific location in the layout |
Partial view |
The child fragment markers that are used to share between views. Partial views can also contain inline code, HTML helper methods, and references to other partial views. Partial views do not invoke action methods, so they cannot be used to perform transaction logic |
Child action |
Used to create reusable UI controls, or to require widgets that contain transactional logic. When a child action is used, it invokes an action method, returns a view, and injects the result into the response stream |
1. Segmentation (Section): provides a content area for a layout. Inserts a portion of the view into the layout.
When Razor parses a layout, the Rendersection helper method displays the segmented contents of the specified name in the view. Segmented content that is not included in the view is inserted where the Renderbody helper is used in the layout.
Note: A view can only define segments that are referenced in a layout. An exception is thrown if you attempt to define a segment in a view that does not have a corresponding @rendersection helper call in the layout.
(1) layout (_layout.cshtml) to determine whether a segment is defined in the view:
@if (issectiondefined ("header")) { @RenderSection ("header ")} else { }
(2) Optional segment: @RenderSection ("script", false)
2. Partial view (section): many different places in the application use the same razor tags and html tag fragments.
Partial views render content in the layout.
Partial View strongpartial.cshtml
@model ienumerable<string> <div> The information here is from the partial view <ul> @ foreach (string in Model) { <li> @str </li> } </ul> </div>
Using partial views: in List.html
@{Viewbag.title="List"; Layout=NULL; } @* @Html. Partial ("mypartial")*@ @Html. Partial ("strongpartial",New[] { "Apple","Orange","Pear" })
3. Child action: The action method that is called through the view. A controller logic is used in multiple places in the application.
(1) Define the sub-action method:
[childactiononly] public actionresult time (DateTime time) { return Partialview (time); }
To add a child action view:
@model DateTime <p> Present time: @Model. toshorttimestring () </p>
(2) Render child action:
@Html. Action ("Time", "Home", New{time=datetime.now)
18th Chapter View