gpt4 book ai didi

asp.net-mvc - 关于 ViewEngines.Engines.FindView 方法和 masterName 参数的问题

转载 作者:行者123 更新时间:2023-12-02 04:33:49 29 4
gpt4 key购买 nike

如果我将 null 传递给最后一个参数 masterName,则此方法效果很好,我的类中的 View 设置源自 RazorViewEngine 工作,一切都很好。出于好奇,masterName 参数有何用途?我首先想到也许它是用于layout.cshtml,但是;当我向它传递一个布局时,它会抛出异常......关于如何使用它的任何想法,它在寻找什么?

自定义 View 引擎(几乎没有笑)

public class CustomRazorViewEngine : RazorViewEngine
{
private readonly string[] NewViewFormats = new[]
{
"~/Views/Messaging/{0}.cshtml"
};

public CustomRazorViewEngine()
{
base.ViewLocationFormats = base.ViewLocationFormats.Union(NewViewFormats).ToArray();
}
}

public string RenderViewToString(string viewName, object model, ControllerContext controllerContext,
string masterName)
{
if (string.IsNullOrEmpty(viewName))

viewName = controllerContext.RouteData.GetRequiredString("action");
controllerContext.Controller.ViewData.Model = model;
using (var stringWriter = new StringWriter())
{
ViewEngineResult viewEngineResult = ViewEngines.Engines.FindView(controllerContext, viewName, masterName);
var viewContext = new ViewContext(controllerContext, viewEngineResult.View,
controllerContext.Controller.ViewData,
controllerContext.Controller.TempData,
stringWriter);
viewEngineResult.View.Render(viewContext, stringWriter);
return stringWriter.GetStringBuilder().ToString();
}
}

最佳答案

经过更多调试后,我发现了似乎是正确的答案。首先让我声明 masterName 参数是“布局”的名称,也就是说正在渲染的 View 将使用。这里的问题是布局必须能够定位。因此,下面的代码可以按预期工作,而不是我原来的帖子中的 ViewEngine 代码。

public string RenderViewToString(string viewName, object model, ControllerContext controllerContext,
string masterName)
{
if (string.IsNullOrEmpty(viewName))

viewName = controllerContext.RouteData.GetRequiredString("action");
controllerContext.Controller.ViewData.Model = model;
using (var stringWriter = new StringWriter())
{
ViewEngineResult viewEngineResult = ViewEngines.Engines.FindView(controllerContext, viewName, masterName);

var viewContext = new ViewContext(controllerContext, viewEngineResult.View,
controllerContext.Controller.ViewData,
controllerContext.Controller.TempData,
stringWriter);
viewEngineResult.View.Render(viewContext, stringWriter);
return stringWriter.GetStringBuilder().ToString();
}
}

public class CustomRazorViewEngine : RazorViewEngine
{
private readonly string[] NewMasterViewFormats = new[]
{
"~/Views/Messaging/Layouts/{0}.cshtml"
};

private readonly string[] NewViewFormats = new[]
{
"~/Views/Messaging/{0}.cshtml"
};

public CustomRazorViewEngine()
{
base.ViewLocationFormats = base.ViewLocationFormats.Union(NewViewFormats).ToArray();
base.MasterLocationFormats = base.MasterLocationFormats.Union(NewMasterViewFormats).ToArray();
}
}

现在打电话时

string returnViewToString = _viewUtils.RenderViewToString("RegistrationEmail", new RegistrationEmailModel
{ UserName = userName
},
this.ControllerContext,"_RegistrationEmailLayout");

一切都很顺利,并且我的传入 View 的布局(如果它存在于文件夹中)会被使用。这是我这一天的亮点...哈哈

关于asp.net-mvc - 关于 ViewEngines.Engines.FindView 方法和 masterName 参数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7364000/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com