日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET MVC 在控制器中获取某个视图动态的HTML代码

發布時間:2023/12/13 asp.net 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET MVC 在控制器中获取某个视图动态的HTML代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

如果我們需要動態的用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();}}}

?

轉載于:https://www.cnblogs.com/yxlblogs/p/3607276.html

總結

以上是生活随笔為你收集整理的ASP.NET MVC 在控制器中获取某个视图动态的HTML代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。