ASP. net mvc view (1)

Source: Internet
Author: User

ASP. net mvc view (1)
ASP. net mvc view (1)Preface

Starting from this article, I entered the view section in MVC. in the previous sections, I described the use of some objects in views and views more or less, after all, the view is not comprehensive. This article first explains the definition and use of the custom view engine, so that you can learn about the working process of the view engine and View module.

ASP. net mvc View Simple Example of custom view Engine Razor view engine Execution Process Razor view dependency injection and custom view guides Use of segment and segment views Razor syntax and view helper Custom view Engine

Before explaining the custom view engine, let's take a look at some of the object types involved.

First, let's take a look at the definition of the IViewEngine interface type:

Code 1-1

    public interface IViewEngine    {        ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache);        ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache);        void ReleaseView(ControllerContext controllerContext, IView view);    }

In code 1-1, we can see that three methods are defined in the IViewEngine interface type. The first parameter in the first FindPartialView () method is the Controller context type, it contains ViewData and ViewBag information. The second string type parameter is the name of the segment view, and the third parameter is a Boolean parameter indicating whether the current information is cached.

The FindView () method is similar to the FindPartialView () method, but a viewName parameter is added to indicate the view name.

The actual implementation here is to return the IView [view processing type] corresponding to the view engine type in the two methods based on different view engine types ], this part will be explained in the following section.

The ReleaseView () method is used to release resources that process views in IView.

The ViewEngineResult type is an operation return type that encapsulates the IViewEngine and IView types. The return types of the preceding two methods are both ViewEngineResult types.

Code 1-2

public class ViewEngineResult{        public ViewEngineResult(IEnumerable
 
   searchedLocations);        public ViewEngineResult(IView view, IViewEngine viewEngine);        public IEnumerable
  
    SearchedLocations { get; }        public IView View { get; }        public IViewEngine ViewEngine { get; }    }
  
 

In code 1-2, we can see two constructors of the ViewEngineResult type. The first enumerated string type represents a set of view location addresses, the second is object encapsulation.

Let's take a look at the definition of IView:

Code 1-3

Public interface IView {// Abstract: // use the specified writer object to present the specified view context. //// Parameter: // viewContext: // view context. //// Writer: // writer object. Void Render (ViewContext viewContext, TextWriter writer );}

In my understanding, the IView type is the view processing type, which only represents a type of view. For example, the Razor view is a file in cshtml format, and the corresponding IView is the RazorView processing type, this article will be discussed later.

First, let's take a look at the examples below:

Figure 1

The general process is like this. First, when the Controller method returns the ViewResult, The ViewResult will read the IViewEngine from the system's IViewEngine set, and execute FindView for each IViewEngine [if it is a view ], if an IViewEngine returns the ViewEngineResult type, it will stop and run the Render () method of IView in the ViewEngineResult type. The presentation of the final view is not the responsibility of the MVC part. Now let's take a look at the example. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + fuse + sLrMS00PC9wPgoKPHByZSBjbGFzcz0 = "brush: java;"> using System. web. mvc; using MvcApplication. customView; namespace MvcApplication. customViewEngine {public class MyCustomViewEngine: IViewEngine {public ViewEngineResult extends (ControllerContext controllerContext, string partialViewName, bool useCache) {return new evaluate (new string [] {"MyCustomView "});} public ViewEngineResult FindView (ControllerContext controllerContext, string viewName, string masterName, bool useCache) {if (viewName = "MyCustomView") {return new ViewEngineResult (new MyCustomView (), this );} else {return new ViewEngineResult (new string [] {"MyCustomView"}) ;}} public void ReleaseView (ControllerContext controllerContext, IView view ){}}}

The FindView () method implements a basic code. If the view name is "MyCustomView ", the MyCustomView view processing type is returned as the constructor parameter of the ViewEngineResult type.

Custom IView:

Code 1-5

using System.Web.Mvc;namespace MvcApplication.CustomView{    public class MyCustomView:IView    {        public void Render(ViewContext viewContext, System.IO.TextWriter writer)        {            foreach (string key in viewContext.ViewData.Keys)            {                writer.Write("Key:" + key + ",Value:" + viewContext.ViewData[key] + ".

"); } } }}

The definition in code 1-5 is to simply write ViewData values to the writer and render them on the view page.

Finally, we need to add the custom view engine to the view engine set of the system, in the Application_Start () method of the Global. asax file.

Code 1-6

ViewEngines.Engines.Insert(0, new CustomViewEngine.MyCustomViewEngine());

The method of adding this is not much to be said. The previous article has already said about this mode, so that the user-defined ranking is the first in the set.

Finally, the arbitrary code in a controller method is changed to the following code:

Code 1-7

        public ActionResult CustomView()        {            this.ViewData.Add("DebugData", "Jinyuan");            this.ViewData.Add("DebugDate", "2014-01-01");                     return View("MyCustomView");        }

Figure 2


Author: Jin Yuan

Source: http://blog.csdn.net/jinyuan0829

The copyright of this article is shared by the author and CSDN. You are welcome to repost this article, but you must keep this statement without the author's consent.

Related Article

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.