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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM