Mvc4 switches View templates based on domain names, and mvc4view templates

Source: Internet
Author: User

Mvc4 switches View templates based on domain names, and mvc4view templates

Rewrite template search method:

public class MyRazorViewEngine : RazorViewEngine    {        public MyRazorViewEngine() : base()        {        }        private const  string DefaultView = "~/Views/";        private const string MoblieDomain = "m.";        private const string WwwFolder = "~/Views/www/";        private const string MoblieFolder = "~/Views/mobile/";        public static string GetView(HttpRequestBase request)        {            if (request.Url == null) return DefaultView;            var url = request.Url.Host;            return url.Contains(MoblieDomain) ? MoblieFolder : WwwFolder;        }        string GetView(ControllerContext controllerContext)        {            return GetView(controllerContext.HttpContext.Request);        }        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)        {            var views = GetView(controllerContext);            return base.CreatePartialView(controllerContext, partialPath.Replace(DefaultView,views));        }        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)        {            var views = GetView(controllerContext);            return base.CreateView(controllerContext, viewPath.Replace(DefaultView, views),                masterPath.Replace(DefaultView, views));        }        protected override bool FileExists(ControllerContext controllerContext, string virtualPath)        {            var views = GetView(controllerContext);            return base.FileExists(controllerContext, virtualPath.Replace(DefaultView, views));        }    }

Application_Start () Registration

ViewEngines.Engines.Add(new MyRazorViewEngine());

_ ViewStart. cshtml

@ {Layout = "~ /Views/Shared/_ Layout. cshtml ". Replace ("~ /Views/", MyRazorViewEngine. GetView (Request ));}

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.