ASP.NET MVC 在控制器中擷取某個視圖動態HTML代碼

來源:互聯網
上載者:User

如果我們需要動態用AJAX從伺服器端擷取HTML代碼,拼接字串是一種不好的方式,所以我們將HTML代碼寫在cshtml檔案中,然後通過代碼傳入model,動態擷取cshtml中的HTML代碼

當然,我們想要使用通用的方法去擷取cshtml,就必須重寫RazorViewEngine視圖引擎,配置視圖搜尋位置

在尋找一個視圖時,Razor視圖引擎遵循了MVC架構早期版本建立起來的約定。例如,如果你請求與Home控制器相關的Index視圖,Razor會審查這樣的視圖列表:

 ~/Views/Home/Index.cshtml
● ~/Views/Home/Index.vbhtml
● ~/Views/Shared/Index.cshtml
● ~/Views/Shared/Index.vbhtml

正如你現在知道的,Razor實際上不會在磁碟上尋找這些視圖檔案,因為它們還沒有被編譯成C#類。Razor尋找的是表示這些視圖的編譯類。.cshtml檔案是含有C#語句的模板(我們正在使用的這種),而.vbhtml檔案含有Visual Basic語句。

你可以通過產生一個RazorViewEngine子類,來改變Razor搜尋的這種視圖檔案。這個類是Razor的IViewEngine實現。它建立於一組基類之上,這些類定義一組用來確定搜尋哪種視圖檔案的屬性。這些屬性如表所描述。

Property
屬性
Description
描述
Default Value
預設值
ViewLocationFormats
MasterLocationFormats
PartialViewLocationFormats
The locations to look for views, partial views, and layouts
查詢檢視表、分部視圖、以及布局的位置
"~/Views/{1}/{0}.cshtml",
"~/Views/{1}/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.vbhtml"
AreaViewLocationFormats
AreaMasterLocationFormats
AreaPartialViewLocationFormats
The locations to look for views, partial views, and layouts for an area
尋找一個地區的視圖、分部視圖、及布局的位置
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/{1}/{0}.vbhtml",
"~/Areas/{2}/Views/Shared/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.vbhtml"

 這些屬性先於Razor的引入,這是每組三個屬性具有相同值的原因。每個屬性是一個字串數組,它們是用複合字元串格式化符號來表示的。以下是與預留位置對應的參數值:
● {0} represents the name of the view.
{0} 表示視圖名
● {1} represents the name of the controller.
{1} 表示控制器名
● {2} represents the name of the area.
{2} 表示地區名
為了修改搜尋位置,你要產生一個派生於RazorViewEngine的新類,並修改表所描述的一個或多個屬性值。

在Infrastructure檔案夾中建立一個CustomRazorViewEngine類

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace MvcApplication1.Infrastructure{    public class CustomRazorViewEngine : RazorViewEngine    {        public CustomRazorViewEngine()        {            ViewLocationFormats = new string[] {                "~/Views/{1}/{0}.cshtml",                 "~/Views/Shared/{0}.cshtml",                  "~/Views/Shared_PartialView/{0}.cshtml"//指定尋找某個檔案的路徑            };            PartialViewLocationFormats = new string[] {                "~/Views/{1}/{0}.cshtml",                 "~/Views/Shared/{0}.cshtml",                  "~/Views/Shared_PartialView/{0}.cshtml"////指定尋找某個檔案的路徑            };        }    }}

我們在Global.asax的Application_Start方法中,用ViewEngines.Engines集合來註冊我們的這個派生視圖引擎,像這樣:

protected void Application_Start()        {            AreaRegistration.RegisterAllAreas();            ViewEngines.Engines.Clear();            ViewEngines.Engines.Add(new CustomRazorViewEngine());            WebApiConfig.Register(GlobalConfiguration.Configuration);            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);            RouteConfig.RegisterRoutes(RouteTable.Routes);            BundleConfig.RegisterBundles(BundleTable.Bundles);        }

擷取html字串的方法以及如何調用

public class HomeController : Controller    {        //        // GET: /Home/             public ActionResult Index()        {            string html =  this.ControllerContext.RenderViewToString("_CommonPartial", new UserViewModel() {  UserName="haha"});            return View(new UserViewModel() { IsEnable = false, UserCode = "aa" });        }          }    public static class HelperExtensions    {        public static string RenderViewToString(this ControllerContext context, string viewName, object model)        {            if (string.IsNullOrEmpty(viewName))                viewName = context.RouteData.GetRequiredString("action");            context.Controller.ViewData.Model = model;            using (var sw = new StringWriter())            {                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(context, viewName);                var viewContext = new ViewContext(context,                                                  viewResult.View,                                                  context.Controller.ViewData,                                                  context.Controller.TempData,                                                  sw);                try                {                    viewResult.View.Render(viewContext, sw);                }                catch (Exception ex)                {                    throw;                }                return sw.GetStringBuilder().ToString();            }        }    }


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.