Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of ASP.NET MVC rewriting

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "sample analysis of ASP.NET MVC rewriting", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of ASP.NET MVC rewriting".

There are generally two ways to switch topics in ASP.NET MVC, one is to switch between css and js references to the skin, and the other is to rewrite the view engine. The way to rewrite the view engine is more flexible, because not only can I have different layouts and styles under different themes, but I can also make the data entries displayed under different themes inconsistent, which means you can add something personalized under some themes.

I'm going to demonstrate by rewriting the view engine, and before I do that, I assume you already have some basics of MVC, let's take a look at the results:

The default theme is after the system logs in, and when we click to switch the theme, the layout of the left menu bar changes, the style of the right content changes, and the address bar remains the same. The interface UI uses metronic, although the official website is charged, but in China, you can always find it for free. Official address: http://keenthemes.com/preview/metronic/

Here, I use a sub-region, sub-module (divided by independent business functions), each module is an independent dll, where Secom.Emx.Admin and Secom.Emx.History are two independent modules, and create regions Admin and History respectively.

You will find that the Areas directory under the Secom.Emx.Admin model is exactly the same as the directory in Secom.Emx.WebApp. In fact, I didn't want to add any View to the module project at first, but I did so to facilitate stand-alone deployment.

Xcopy / e/r/y $(ProjectDir) Areas\ Admin\ Views $(SolutionDir) Secom.Emx.WebApp\ Areas\ Admin\ Views

This command is simple: when compiling the project Secom.Emx.Admin, copy the Views in the project to the specified directory of the Secom.Emx.WebApp project.

The AdminAreaRegistration code is as follows:

Using System.Web.Mvc;namespace Secom.Emx.WebApp {public class AdminAreaRegistration: AreaRegistration {public override string AreaName {get {return "Admin" } public override void RegisterArea (AreaRegistrationContext context) {context.MapRoute ("Admin_default", "Admin/ {controller} / {action} / {id}", new {action = "Index", id = UrlParameter.Optional}, namespaces:new string [1] {"Secom.Emx.Admin.Areas.Admin.Controllers"});}}

Notice the namespace and the subsequent addition of namespaces:new string [1] {"Secom.Emx.Admin.Areas.Admin.Controllers"}, which is the namespace where the controller under the stand-alone module Secom.Emx.Admin is located.

The HistoryAreaRegistration code is as follows:

} public override void RegisterArea (AreaRegistrationContext context) {context.MapRoute ("History_default", "History/ {controller} / {action} / {id}", new {action = "Index", id = UrlParameter.Optional}, namespaces:new string [1] {"Secom.Emx.History.Areas.History.Controllers"});}}

Let's take a look at the original constructor of RazorViewEngine as follows:

Public RazorViewEngine (IViewPageActivator viewPageActivator): base (viewPageActivator) {AreaViewLocationFormats = new [] {"~ / Areas/ {2} / Views/ {1} / {0} .cshtml", "~ / Areas/ {2} / Views/ {1} / {0} .vbhtml", "~ / Areas/ {2} / Views/Shared/ {0} .cshtml", "~ / Areas/ {2} / Views/Shared/ {0} .vbhtml"} AreaMasterLocationFormats = new [] {"~ / Areas/ {2} / Views/ {1} / {0} .cshtml", "~ / Areas/ {2} / Views/ {1} / {0} .vbhtml", "~ / Areas/ {2} / Views/Shared/ {0} .cshtml", "~ / Areas/ {2} / Views/Shared/ {0} .vbhtml"} AreaPartialViewLocationFormats = new [] {"~ / Areas/ {2} / Views/ {1} / {0} .cshtml", "~ / Areas/ {2} / Views/ {1} / {0} .vbhtml", "~ / Areas/ {2} / Views/Shared/ {0} .cshtml", "~ / Areas/ {2} / Views/Shared/ {0} .vbhtml"} ViewLocationFormats = new [] {"~ / Views/ {1} / {0} .cshtml", "~ / Views/ {1} / {0} .vbhtml", "~ / Views/Shared/ {0} .cshtml", "~ / Views/Shared/ {0} .vbhtml"} MasterLocationFormats = new [] {"~ / Views/ {1} / {0} .cshtml", "~ / Views/ {1} / {0} .vbhtml", "~ / Views/Shared/ {0} .cshtml", "~ / Views/Shared/ {0} .vbhtml"} PartialViewLocationFormats = new [] {"~ / Views/ {1} / {0} .cshtml", "~ / Views/ {1} / {0} .vbhtml", "~ / Views/Shared/ {0} .cshtml", "~ / Views/Shared/ {0} .vbhtml"}; FileExtensions = new [] {"cshtml", "vbhtml",};}

Then the new CustomRazorViewEngine inherits from RazorViewEngine and rewrites the routing rules of View. Since you can rewrite routing rules, that means you can define rules at will and follow your own rules. It should be noted that we should pay attention to the order in the routing array. When looking for the view, we look it up in the order before and after. When we find the view, we will return immediately, and we will not match the following routing rules. In order to improve the efficiency of route lookup, I deleted all vbhtml routing rules here, because I used the C # language throughout my project.

Using System.Web.Mvc Namespace Secom.Emx.WebApp.Helper {public class CustomRazorViewEngine: RazorViewEngine {public CustomRazorViewEngine (string theme) {if (! string.IsNullOrEmpty (theme)) {AreaViewLocationFormats = new [] {/ themes "~ / themes/" + theme+ "/ views/Areas/ {2} / {1} / {0} .cshtml", "~ / themes/" + theme+ "/ Shared/ {0} .cshtml"~ / Areas/ {2} / Views/ {1} / {0} .cshtml" "~ / Areas/ {2} / Views/Shared/ {0} .cshtml"} AreaMasterLocationFormats = new [] {/ / themes "~ / themes/" + theme+ "/ views/Areas/ {2} / {1} / {0} .cshtml", "~ / themes/" + theme+ "/ views/Areas/ {2} / Shared/ {0} .cshtml", "~ / themes/" + theme+ "/ views/Shared/ {0} .cshtml", "~ / Areas/ {2} / Views/ {1} / {0} .cshtml" "~ / Areas/ {2} / Views/Shared/ {0} .cshtml"} AreaPartialViewLocationFormats = new [] {/ / themes "~ / themes/" + theme+ "/ views/Shared/ {0} .cshtml", "~ / Areas/ {2} / Views/ {1} / {0} .cshtml", "~ / Areas/ {2} / Views/Shared/ {0} .cshtml"} ViewLocationFormats = new [] {/ / themes "~ / themes/" + theme+ "/ views/ {1} / {0} .cshtml", "~ / Views/ {1} / {0} .cshtml", "~ / Views/Shared/ {0} .cshtml"} MasterLocationFormats = new [] {/ / themes "~ / themes/" + theme+ "/ views/Shared/ {0} .cshtml", "~ / Views/ {1} / {0} .cshtml", "~ / Views/Shared/ {0} .cshtml"} PartialViewLocationFormats = new [] {/ / themes "~ / themes/" + theme+ "/ views/Shared/ {0} .cshtml", "~ / Views/ {1} / {0} .cshtml", "~ / Views/Shared/ {0} .cshtml"}; FileExtensions = new [] {"cshtml"};}

After rewriting, our routing rule will be like this: if no topic is selected, the original routing rule will be used, and if the topic is selected, the rewritten routing rule will be used.

New routing rule: if a topic is selected, first look for thems/ topic name / views/Areas/ area name / controller name / view name .cshtml. If you can't find it, follow the default routing rule, that is, Areas/ area name / Views/ controller name / view name. Cshtml

Toggle theme View code:

Toggle theme default theme blue theme

Function setTheme (themeName) {_ window.location.href = "/ Home/SetTheme?themeName=" + themeName + "& href=" + _ window.location.href;}

When the user logs in successfully, the selected topic information is read from the Cookie, and when the topic record is not read in the Cookie, the configured topic name is read from the Web.config configuration file. If none is read, it is the default theme and follows the original view engine rules.

In the background management interface, every time I select a theme, I store the theme name in Cookie, which is saved by default for one year, so that the next time I log in, I can remember the selected theme information.

Using System;using System.Web.Mvc;using Secom.Emx.WebApp.Helper;using System.Web;using Secom.Emx.Common.Controllers;namespace Secom.Emx.WebApp.Controllers {public class HomeController: BaseController {string themeCookieName = "Theme"; public ActionResult Index () {ViewData ["Menu"] = GetMenus (); return View () } public ActionResult SetTheme (string themeName,string href) {if (! string.IsNullOrEmpty (themeName)) {Response.Cookies.Set (new HttpCookie (themeCookieName, themeName) {Expires = DateTime.Now.AddYears (1)});} else {themeName = Request.Cookies [themeCookieName] .value? "" .Trim ();} Utils.ResetRazorViewEngine (themeName); return string.IsNullOrEmpty (href)? Redirect ("~ / Home/Index"): Redirect (href);} public ActionResult Login () {string themeName = Request.Cookies [themeCookieName] .value? "" .Trim (); if (! string.IsNullOrEmpty (themeName)) {Utils.ResetRazorViewEngine (themeName);} return View ();}

Utils class:

Using System.Configuration;using System.Web.Mvc;namespace Secom.Emx.WebApp.Helper {public class Utils {private static string _ themeName; public static string ThemeName {get {if (! string.IsNullOrEmpty (_ themeName)) {return _ themeName;} / / template style _ themeName = string.IsNullOrEmpty (ConfigurationManager.AppSettings ["Theme"])? "": ConfigurationManager.AppSettings ["Theme"]; return _ themeName }} public static void ResetRazorViewEngine (string themeName) {themeName = string.IsNullOrEmpty (themeName)? Utils.ThemeName: themeName; if (! string.IsNullOrEmpty (themeName)) {ViewEngines.Engines.Clear (); ViewEngines.Engines.Add (new CustomRazorViewEngine (themeName));} above is all the content of the article "sample Analysis of ASP.NET MVC rewriting". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report