How to create custom viewengine in MVC 2

Source: Internet
Author: User

As is we know, by default, the viewengine of MVC is webformsviewengine. After refector the source code, I find that the engine implementIviewAndVirtualpathproviderviewengine. So we also can create custom engine.

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Xml;using System.Xml.Xsl;using System.Xml.Linq;namespace MvcApplication1.Models{    public class XSLTViewEngine:VirtualPathProviderViewEngine    {        public XSLTViewEngine() {            ViewLocationFormats = PartialViewLocationFormats = new[] {                "~/Views/{1}/{0}.xslt","~/Views/Shared/{0}.xslt"            };        }        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)        {            return new XSLTView(controllerContext, partialPath);        }        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)        {            return new XSLTView(controllerContext, viewPath);        }    }    public class XSLTView : IView    {        private readonly XslCompiledTransform _template;        public XSLTView(ControllerContext context, string viewPath)        {            _template = new XslCompiledTransform();            _template.Load(context.HttpContext.Server.MapPath(viewPath));        }        #region IView Members        public void Render(ViewContext viewContext, System.IO.TextWriter writer)        {            XDocument xmlModel = viewContext.ViewData.Model as XDocument;            if (xmlModel != null)            {                _template.Transform(xmlModel.CreateReader(), null, writer);            }            else throw new ArgumentException("Invalid xdocument");        }        #endregion    }}

After that, we shoshould register the viewengine in global. asax.

 protected void Application_Start()        {            AreaRegistration.RegisterAllAreas();            RegisterRoutes(RouteTable.Routes);            ViewEngines.Engines.Clear();            ViewEngines.Engines.Add(new XSLTViewEngine());        }

Of course, we can sepecify The Engin in controller.

        public ActionResult Index()        {            ViewResult result =  View(GetBooks());            result.ViewEngineCollection = new ViewEngineCollection(){               new XSLTViewEngine()             };            return result;        }

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.