溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ASP.NET MVC重寫的示例分析

發(fā)布時(shí)間:2021-09-16 18:22:23 來源:億速云 閱讀:153 作者:小新 欄目:編程語言

這篇文章主要為大家展示了“ASP.NET MVC重寫的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“ASP.NET MVC重寫的示例分析”這篇文章吧。

在ASP.NET MVC中來實(shí)現(xiàn)主題的切換一般有兩種方式,一種是通過切換皮膚的css和js引用,一種就是通過重寫視圖引擎。通過重寫視圖引擎的方式更加靈活,因?yàn)槲也粌H可以在不同主題下面布局和樣式不一樣,還可以讓不同的主題下面顯示的數(shù)據(jù)條目不一致,就是說可以在某些主題下面添加一下個(gè)性化的東西。

我將通過重寫視圖引擎的方式來進(jìn)行演示,在這之前,我假設(shè)你已經(jīng)具備了MVC的一些基礎(chǔ),我們先來看下效果:

系統(tǒng)登錄后是默認(rèn)主題,當(dāng)我們點(diǎn)擊切換主題之后,左側(cè)菜單欄的布局變了,右側(cè)內(nèi)容的樣式也變了,而地址欄是不變的。界面UI用的metronic,雖然官網(wǎng)是收費(fèi)的,但是在天朝,總是可以找到免費(fèi)的。官方地址:http://keenthemes.com/preview/metronic/

ASP.NET MVC重寫的示例分析

ASP.NET MVC重寫的示例分析

在這里,我使用了分區(qū)域、分模塊(按獨(dú)立的業(yè)務(wù)功能劃分)的方式,一個(gè)模塊就是一個(gè)獨(dú)立的dll,在這里Secom.Emx.Admin和Secom.Emx.History就是兩個(gè)獨(dú)立的模塊,并分別創(chuàng)建了區(qū)域Admin和History。

ASP.NET MVC重寫的示例分析ASP.NET MVC重寫的示例分析

你會(huì)發(fā)現(xiàn)Secom.Emx.Admin模型下面的Areas目錄和Secom.Emx.WebApp中的目錄是一模一樣的,其實(shí)我最初不想在模塊項(xiàng)目中添加任何的View,但是為了方便獨(dú)立部署還是加了。

右鍵單擊項(xiàng)目Secom.Emx.Admin,選擇“屬性”——“生成事件”添加如下代碼:

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

這命令很簡(jiǎn)單,其實(shí)就是當(dāng)編譯項(xiàng)目Secom.Emx.Admin的時(shí)候,將項(xiàng)目中的Views復(fù)制到Secom.Emx.WebApp項(xiàng)目的指定目錄下。

ASP.NET MVC重寫的示例分析

區(qū)域配置文件我放置到了Secom.Emx.WebApp中,其實(shí)你完全可以獨(dú)立放置到一個(gè)類庫(kù)項(xiàng)目中,因?yàn)樽?cè)區(qū)域路由的后,項(xiàng)目最終會(huì)尋找bin目錄下面所有繼承了AreaRegistration類的,然后讓W(xué)ebApp引用這個(gè)類庫(kù)項(xiàng)目,Secom.Emx.WebApp項(xiàng)目添加Secom.Emx.Admin、Secom.Emx.History的引用。

ASP.NET MVC重寫的示例分析

AdminAreaRegistration代碼如下:

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" }
   );
  }
 }
}

注意命名空間和后面添加的 namespaces:new string[1] { "Secom.Emx.Admin.Areas.Admin.Controllers" },這個(gè)命名空間就是獨(dú)立模塊Secom.Emx.Admin下面的控制器所在的命名空間。

HistoryAreaRegistration代碼如下:

using System.Web.Mvc;

namespace Secom.Emx.WebApp
{
 public class HistoryAreaRegistration : AreaRegistration 
 {
  public override string AreaName 
  {
   get 
   {
    return "History";
   }
  }

  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" }
   );
  }
 }
}

我們先看下RazorViewEngine的原始構(gòu)造函數(shù)如下:

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", 
  }; 
 }

然后新建CustomRazorViewEngine繼承自RazorViewEngine,對(duì)View的路由規(guī)則進(jìn)行了重寫,既然可以重寫路由規(guī)則,那意味著,你可以任意定義規(guī)則,然后遵守自己定義的規(guī)則就可以了。需要注意的是,要注意路由數(shù)組中的順序,查找視圖時(shí),是按照前后順序依次查找的,當(dāng)找到了視圖就立即返回,不會(huì)再去匹配后面的路由規(guī)則。為了提升路由查找效率,我這里刪除了所有vbhtml的路由規(guī)則,因?yàn)槲艺麄€(gè)項(xiàng)目中都采用C#語言。

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"};
   }

  }
 }
}

ASP.NET MVC重寫的示例分析

重寫后,我們的路由規(guī)則將是這樣的:當(dāng)沒有選擇主題的情況下,沿用原來的路由規(guī)則,如果選擇了主題,則使用重寫后的路由規(guī)則。

新的路由規(guī)則:在選擇了主題的情況下,優(yōu)先查找thems/主題名稱/views/Areas/區(qū)域名稱/控制器名稱/視圖名稱.cshtml,如果找不到再按照默認(rèn)的路由規(guī)則去尋找,也就是Areas/區(qū)域名稱/Views/控制器名稱/視圖名稱.cshtml

ASP.NET MVC重寫的示例分析

切換主題View代碼:

<p class="btn-group">
     <button type="button" class="btn btn-circle btn-outline red dropdown-toggle" data-toggle="dropdown">
      <i class="fa fa-plus"></i> 
      <span class="hidden-sm hidden-xs">切換主題 </span> 
      <i class="fa fa-angle-down"></i>
     </button>
     <ul class="dropdown-menu" role="menu">
      <li>
       <a href="javascript:setTheme('default')">
        <i class="icon-docs"></i> 默認(rèn)主題
       </a>
      </li>
      <li>
       <a href="javascript:setTheme('Blue')">
        <i class="icon-tag"></i> 藍(lán)色主題
       </a>
      </li>
     </ul>
    </p>
  <script type="text/javascript">
   function setTheme(themeName)
   {
    window.location.href = "/Home/SetTheme?themeName=" + themeName + "&href=" + window.location.href;
   }
</script>

當(dāng)用戶登錄成功的時(shí)候,從Cookie中讀取所選主題信息,當(dāng)Cookie中沒有讀取到主題記錄時(shí),則從Web.config配置文件中讀取配置的主題名稱,如果都沒有讀取到,則說明是默認(rèn)主題,沿用原有的視圖引擎規(guī)則。

在后臺(tái)管理界面,每次選擇了主題,我都將主題名稱存儲(chǔ)到Cookie中,默認(rèn)保存一年,這樣當(dāng)下次再登錄的時(shí)候,就能夠記住所選的主題信息了。

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類:

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;
    }
    //模板風(fēng)格
    _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));
   }
  }
 }
}

以上是“ASP.NET MVC重寫的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI