Go ASP. NET MVC Dynamic Themes

Source: Internet
Author: User
Tags httpcontext

This article transferred from: Http://www.codeproject.com/Articles/32847/ASP-NET-MVC-Dynamic-Themes

    • Download Source code-148 KB
Introduction

I really needed to enable themes for my application, and I found a interesting article about it by Chris Pietschmann. In my point of view, the only problem and his approach are that need to repeat each page for all themes. Well, I is want to create one page per theme, and has a master page and CSS files for each theme.

Using the Code

For this project, I made some assumptions:

    • The master page for each theme has the name Site.master.
    • The master page file and CSS files is placed on a folder with the name of the theme.
    • All the theme folders is placed in the Content folder.

The file structure would be as this:

Now, let's write a custom View Engine that would inherit from WebFormViewEngine . This code was only a sketch of that want to implement. For instance, I want to save the user theme selection so, in the next login, the user have his option set.

Hide Shrink Copy Code
PublicClass themeviewengine:webformviewengine{Public Themeviewengine () {Base. Viewlocationformats =NewString[] {"~/views/{1}/{0}.aspx ","~/views/{1}/{0}.ascx ","~/views/shared/{0}.aspx ","~/views/shared/{0}.ascx "};Base. Masterlocationformats =NewString[] {"~/content/{2}/site.master "};Base. Partialviewlocationformats =NewString[] {"~/views/{1}/{0}.aspx ","~/views/{1}/{0}.ascx ","~/views/shared/{0}.aspx ","~/views/shared/{0}.ascx "}; }PublicOverride Viewengineresult Findview (ControllerContext controllercontext,String ViewName,String mastername) {if (ControllerContext = =NULL) {ThrowNew ArgumentNullException ("ControllerContext "); }if (String. IsNullOrEmpty (ViewName)) {ThrowNew ArgumentException ("Value is required. ","ViewName "); }String themename =This. Getthemetouse (ControllerContext);String[] searchedviewlocations;String[] searchedmasterlocations;String controllername = ControllerContext.RouteData.GetRequiredString ("Controller ");String ViewPath =This. Getviewpath (This. Viewlocationformats, ViewName, Controllername,Out searchedviewlocations);String Masterpath =This. Getmasterpath (This. Masterlocationformats, ViewName, Controllername, ThemeName,Out searchedmasterlocations);if (! (String. IsNullOrEmpty (ViewPath)) && (! ( Masterpath = =String. Empty) | |String. IsNullOrEmpty (Mastername))) {ReturnNew Viewengineresult ((This. CreateView (ControllerContext, ViewPath, Masterpath)),this); }ReturnNew Viewengineresult (Searchedviewlocations.union<string> (searchedmasterlocations)); }PublicOverride Viewengineresult Findpartialview (ControllerContext controllercontext,String partialviewname) {if (ControllerContext = =NULL) {ThrowNew ArgumentNullException ("ControllerContext "); }if (String. IsNullOrEmpty (Partialviewname)) {ThrowNew ArgumentException ("Value is required. ", partialviewname); }String themename =This. Getthemetouse (ControllerContext);String[] searchedlocations;String controllername = ControllerContext.RouteData.GetRequiredString ("Controller ");String Partialpath =This. Getviewpath (This. Partialviewlocationformats, Partialviewname, Controllername,Out searchedlocations);if (String. IsNullOrEmpty (Partialpath)) {ReturnNew Viewengineresult (searchedlocations); }ReturnNew Viewengineresult (This. Createpartialview (ControllerContext, Partialpath),this); }PrivateString Getthemetouse (ControllerContext controllercontext) {if (ControllerContext.HttpContext.Request.QueryString.AllKeys.Contains ("Theme ")) {String themename = controllercontext.httpcontext.request.querystring["Theme "]; CONTROLLERCONTEXT.HTTPCONTEXT.SESSION.ADD ("Theme ", themename); }Elseif (controllercontext.httpcontext.session["Theme "] = =NULL) {CONTROLLERCONTEXT.HTTPCONTEXT.SESSION.ADD ("Theme ","Default "); }Return controllercontext.httpcontext.session["Theme "]. ToString (); }PrivateString Getviewpath (String[] locations,String ViewName,String Controllername,OutString[] searchedlocations) {String path =Null Searchedlocations =NewString[locations. Length];for (int i =0; I < locations. Length; i++) {Path =String. Format (CultureInfo.InvariantCulture, Locations[i),NewObject[] {viewName, controllername});if (This. Virtualpathprovider.fileexists (Path)) {searchedlocations =Newstring[0];return path; } Searchedlocations[i] = path; }ReturnNull }PrivateString Getmasterpath (String[] locations,String ViewName,String Controllername,String ThemeName,OutString[] searchedlocations) {string path = null; searchedlocations = new string[locations. Length]; for (int i = 0; i < locations. Length; i++) {path = string. Format (CultureInfo.InvariantCulture, locations[i], new  Object[] {viewName, controllername, themename}); if (this. Virtualpathprovider.fileexists (Path)) {searchedlocations = new string[0]; return Path;} Searchedlocations[i] = path; } return null;}        

To-finish this sample, just change the Global.asax, removing all engines from the View Engine and adding the Cust Om One:

Hide Copy Code
void Application_Start(){    RegisterRoutes(RouteTable.Routes);    ViewEngines.Engines.Clear();    ViewEngines.Engines.Add(new ThemeViewEngine());}

Well, hope that ' s useful.

&amp;amp;lt;a href= "http://pubads.g.doubleclick.net/gampad/jump?iu=/6839/lqm.codeproject.site/ web-development/asp-net/general&amp;amp;amp;sz=300x250&amp;amp;amp;c=361649 "&amp;amp;gt;&amp; Amp;lt;img src= "Http://pubads.g.doubleclick.net/gampad/ad?iu=/6839/lqm.codeproject.site/Web-Development/ASP-NET /general&amp;amp;amp;sz=300x250&amp;amp;amp;c=361649 "width=" 300px "height=" 250px "target=" _blank "/& Amp;amp;gt;&amp;amp;lt;/a&amp;amp;gt; License

This article, along with any associated source code and files, is licensed under the Code Project Open License (Cpol)

Go ASP. NET MVC Dynamic Themes

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.