ASP. NET view engine and asp.net view Engine

Source: Internet
Author: User

ASP. NET view engine and asp.net view Engine

The Razor view engine is introduced from MVC3.0 in ASP. NET. The legacy ASPX engine is used for maintenance and the old version of MVC programs.

1. View Engine implements the IViewEngine interface. View Engine converts View requests to ViewEngineResult objects. The following is the definition of this interface:

namespace System.Web.Mvc{    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);    }

The support of the View engine in the Mvc framework is implemented by the ControllerActionInvoker class, which is a built-in implementation of the IActionInvoker interface.

 

Ii. ViewEngineResult class allows the view engine to respond to the Mvc framework. One of the two constructors can represent a result. That is to say, it contains an IView or a list of locations used to search for an appropriate view.

using System.Collections.Generic;namespace System.Web.Mvc{    public class ViewEngineResult    {        public ViewEngineResult(IEnumerable<string> searchedLocations)        {            if (searchedLocations == null)            {                throw new ArgumentNullException("searchedLocations");            }            SearchedLocations = searchedLocations;        }        public ViewEngineResult(IView view, IViewEngine viewEngine)        {            if (view == null)            {                throw new ArgumentNullException("view");            }            if (viewEngine == null)            {                throw new ArgumentNullException("viewEngine");            }            View = view;            ViewEngine = viewEngine;        }        public IEnumerable<string> SearchedLocations { get; private set; }        public IView View { get; private set; }        public IViewEngine ViewEngine { get; private set; }    }}

 

The public ViewEngineResult (IView view, IViewEngine viewEngine) constructor accepts an IView interface and a view engine, which allows you to call the ReleaseView method later.

If the view engine cannot provide a view of the request, you can use another public ViewEngineResult (IEnumerable <string> searchedLocations) function to provide it with a list of view locations, if no query is found at these locations, the information is displayed to the user.

Iii. IView interface: Generate client response.

using System.IO;namespace System.Web.Mvc{    public interface IView    {        void Render(ViewContext viewContext, TextWriter writer);    }}

This interface has only one method to get the value of ViewContext and write a response to the client using TextWriter.

 

Iv. Custom view Engine

You can create a custom view engine by implementing the IViewEngine interface. After creating a custom view engine, we also need to register the custom view engine. The Mvck framework provides us with the method of registering in 2.

1. Register with the Application_Start method of Global. asax.

ViewEngines. Engines. Add (new custom view engine ());

This method can control the view engine priority. If you want to add a custom view engine to the first place. Write as follows:

ViewEngines. Engines. Insert (0, new custom view engine ());

Second, register with the dependency parser within the application scope.

This method does not control the view engine priority.

Bytes -----------------------------------------------------------------------------------------------------------

Therefore, we can create a custom view engine by implementing two interfaces (IViewEngine interface and IView interface. The function of this custom view engine is very simple.

The Mvc Framework provides a powerful Razor engine, and third parties also provide us with powerful view engines. Example: Spark, NHaml, Brail, NVelocity


In aspnet mvc30, razor and ASPX can interact with each other.

What are your questions?
Do you want to ask whether the razor view engine and the ASPX view engine can be cross-referenced?
I have not tried this yet. It should be okay.

Aspnet mvc control View query order

This is the default MVC Convention. If you want to change it, you can only create a custom view engine and adjust the priority of the view order when implementing the FindView method.

In addition, if your route parameters are correct and you can directly find the view at one time, you can ignore the default conventions.
 

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.