MVC自定義檢視規則

yuejin發表於2014-11-13

自定義規則:

using System.Web.Mvc;
using System.Configuration;

namespace Research
{
    public class ViewConfig
    {
        public static void RegisterView()
        {
            string temp = ConfigurationManager.AppSettings["ShopTemplate"];
            RazorViewEngine r = new RazorViewEngine();
            r.ViewLocationFormats = new[] { "~/Template/" + temp + "/{1}/{0}.cshtml", "~/Views/{1}/{0}.cshtml" };
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(r);
        }
    }
}

註冊自定義規則:在Global.asax.cs中加入註冊程式碼

public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            //註冊自定義檢視訪問規則
            ViewConfig.RegisterView();
        }

    }

 

相關文章