2012-03-23 10 views
17

Szukam wdrożenia niestandardowego RazorViewEngine. Zasadniczo mam dwie witryny z tą samą bazą kodu. Różnice polegają na tym, że wyglądają inaczej. Chcę zastąpić standardowy mechanizm wyświetlania, aby wygląd MVC był wyświetlany w dwóch oddzielnych lokalizacjach dla widoków, układów itp. Dla firmy A i dla firmy B. Firma A będzie zawierała widoki główne, a widok firmy B zastąpi te wzorce. Dlatego chcę, aby mechanizm wyświetlania wyglądał w lokalizacji B dla widoku, układu, wzorca lub częściowego, jeśli znajdzie go, a następnie zwróci, jeśli go nie znajdzie, chcę, aby domyślnie były to widoki firmy A jako domyślne. Oczywiście firma A zajrzy tylko do swojego folderu.Jak zaimplementować niestandardowy RazorViewEngine, aby znaleźć widoki w niestandardowych lokalizacjach?

Ok do sedna pytania: Znalazłem tę stronę: http://www.aspnetwiki.com/mvc-3-razor:extending-the-view-engine

pierwsze pytanie, czy to jest najlepszym sposobem osiągnięcia tego celu?

drugie muszę zastąpić metody CreatePartial, CreateView, FindPartial i FindView?

Aktualizacja

Ok mam zorientowali się pytanie drugie ja, metod Chcę przesłonić są CreateView i CreatePartialView jak w tym momencie jest zbudowany ciąg widok i mogę bawić się z nim.

+0

Czy chcesz zachować dwie różne witryny w tym samym folderze? –

+0

Chcę powiedzieć, że chcę, aby widoki były w oddzielnych folderach, ale aby mogły korzystać z tego samego kontrolera/modelu. – Liam

Odpowiedz

13

Ok w końcu zdecydowałem się na podejściu szczegółowo tutaj: http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.aspx

dzięki @Adriano za odpowiedzi i wskazówek, ale w końcu myślę, że to podejście lepiej pasuje do moich potrzeb. Poniższe podejście pozwala mi zachować standardową funkcjonalność, ale stworzyć nową wyszukiwaną lokalizację o wyższym priorytecie.

public class Travel2ViewEngine : RazorViewEngine 
{ 
    protected BrandNameEnum BrandName; 
    private string[] _newAreaViewLocations = new string[] { 
     "~/Areas/{2}/%1Views/{1}/{0}.cshtml", 
     "~/Areas/{2}/%1Views/{1}/{0}.vbhtml", 
     "~/Areas/{2}/%1Views//Shared/{0}.cshtml", 
     "~/Areas/{2}/%1Views//Shared/{0}.vbhtml" 
    }; 

    private string[] _newAreaMasterLocations = new string[] { 
     "~/Areas/{2}/%1Views/{1}/{0}.cshtml", 
     "~/Areas/{2}/%1Views/{1}/{0}.vbhtml", 
     "~/Areas/{2}/%1Views/Shared/{0}.cshtml", 
     "~/Areas/{2}/%1Views/Shared/{0}.vbhtml" 
    }; 

    private string[] _newAreaPartialViewLocations = new string[] { 
     "~/Areas/{2}/%1Views/{1}/{0}.cshtml", 
     "~/Areas/{2}/%1Views/{1}/{0}.vbhtml", 
     "~/Areas/{2}/%1Views/Shared/{0}.cshtml", 
     "~/Areas/{2}/%1Views/Shared/{0}.vbhtml" 
    }; 

    private string[] _newViewLocations = new string[] { 
     "~/%1Views/{1}/{0}.cshtml", 
     "~/%1Views/{1}/{0}.vbhtml", 
     "~/%1Views/Shared/{0}.cshtml", 
     "~/%1Views/Shared/{0}.vbhtml" 
    }; 

    private string[] _newMasterLocations = new string[] { 
     "~/%1Views/{1}/{0}.cshtml", 
     "~/%1Views/{1}/{0}.vbhtml", 
     "~/%1Views/Shared/{0}.cshtml", 
     "~/%1Views/Shared/{0}.vbhtml" 
    }; 

    private string[] _newPartialViewLocations = new string[] { 
     "~/%1Views/{1}/{0}.cshtml", 
     "~/%1Views/{1}/{0}.vbhtml", 
     "~/%1Views/Shared/{0}.cshtml", 
     "~/%1Views/Shared/{0}.vbhtml" 
    }; 

    public Travel2ViewEngine() 
     : base() 
    { 
     Enum.TryParse<BrandNameEnum>(Travel2.WebUI.Properties.Settings.Default.BrandName, out BrandName); 

     AreaViewLocationFormats = AppendLocationFormats(_newAreaViewLocations, AreaViewLocationFormats); 

     AreaMasterLocationFormats = AppendLocationFormats(_newAreaMasterLocations, AreaMasterLocationFormats); 

     AreaPartialViewLocationFormats = AppendLocationFormats(_newAreaPartialViewLocations, AreaPartialViewLocationFormats); 

     ViewLocationFormats = AppendLocationFormats(_newViewLocations, ViewLocationFormats); 

     MasterLocationFormats = AppendLocationFormats(_newMasterLocations, MasterLocationFormats); 

     PartialViewLocationFormats = AppendLocationFormats(_newPartialViewLocations, PartialViewLocationFormats); 
    } 

    private string[] AppendLocationFormats(string[] newLocations, string[] defaultLocations) 
    { 
     List<string> viewLocations = new List<string>(); 
     viewLocations.AddRange(newLocations); 
     viewLocations.AddRange(defaultLocations); 
     return viewLocations.ToArray(); 
    } 

    protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) 
    { 
     return base.CreateView(controllerContext, viewPath.Replace("%1", BrandName.ToString()), masterPath); 
    } 

    protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath) 
    { 
     return base.CreatePartialView(controllerContext, partialPath.Replace("%1", BrandName.ToString())); 
    } 

    protected override bool FileExists(ControllerContext controllerContext, string virtualPath) 
    { 
     return base.FileExists(controllerContext, virtualPath.Replace("%1", BrandName.ToString())); 
    } 
} 

następnie zarejestrować w Gloabal.asax

protected void Application_Start(object sender, EventArgs e) 
{ 
    RegisterRoutes(RouteTable.Routes); 


    //Register our customer view engine to control T2 and TBag views and over ridding 
    ViewEngines.Engines.Clear(); 
    ViewEngines.Engines.Add(new Travel2ViewEngine()); 
} 
5

Tak, ale nie trzeba zastępować tych metod. RazorViewEngine dziedziczy po VirtualPathProviderViewEngine, dzięki czemu możesz użyć jego właściwości, aby ustawić położenie swoich widoków.

Na przykład przeczytaj Creating your first MVC ViewEngine i How to set a Default Route (To an Area) in MVC.

+0

To wydaje się interesujące, ale nie jestem przekonany, że robi dokładnie to, co chcę, dokumentacja jest trochę rzadka, czy masz przykład, który możesz opublikować? – Liam

+1

To tylko mały przykład, ale: http://coderjournal.com/2009/05/creating-your-first-mvc-viewengine/ i ten bardzo ładny link tutaj na SO: http://stackoverflow.com/questions/2140208/how-to-set-a-default-route-to-an-area-in-mvc –

+0

Właśnie znalazłem pierwszą! Ta! – Liam

1

Oto moja odpowiedź: Dodaj to do swojej global.ascx

 ViewEngines.Engines.Clear(); 
     var customEngine = new RazorViewEngine(); 
     customEngine.PartialViewLocationFormats = new string[] 
     { 
      "~/Views/{1}/{0}.cshtml", 
      "~/Views/Shared/{0}.cshtml", 
      "~/Views/Partial/{0}.cshtml", 
      "~/Views/Partial/{1}/{0}.cshtml" 
     }; 



     customEngine.ViewLocationFormats = new string[] 
     { 
      "~/Views/{1}/{0}.cshtml", 
      "~/Views/Shared/{0}.cshtml", 
      "~/Views/Controller/{1}/{0}.cshtml" 
     }; 

     customEngine.MasterLocationFormats = new string[] 
     { 
      "~/Views/Shared/{0}.cshtml", 
      "~/Views/Layout/{0}.cshtml" 
     }; 

     ViewEngines.Engines.Add(customEngine); 

to są foldery, w których sprawdza brzytwa swoje poglądy.

Daj mi znać, jeśli to działa.

Powiązane problemy