本文整理汇总了C#中IRenderContext.LocateView方法的典型用法代码示例。如果您正苦于以下问题:C# IRenderContext.LocateView方法的具体用法?C# IRenderContext.LocateView怎么用?C# IRenderContext.LocateView使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IRenderContext
的用法示例。
在下文中一共展示了IRenderContext.LocateView方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RenderView
/// <summary>
/// Renders the view.
/// </summary>
/// <param name="viewLocationResult">A <see cref="ViewLocationResult"/> instance, containing information on how to get the view template.</param>
/// <param name="model">The model that should be passed into the view</param>
/// <param name="renderContext">The render context.</param>
/// <param name="isPartial">Used by HtmlHelpers to declare a view as partial</param>
/// <returns>A response.</returns>
public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext, bool isPartial)
{
Assembly referencingAssembly = null;
if (model != null)
{
var underlyingSystemType = model.GetType().UnderlyingSystemType;
if (underlyingSystemType != null)
{
referencingAssembly = Assembly.GetAssembly(underlyingSystemType);
}
}
var response = new HtmlResponse();
response.Contents = stream =>
{
var writer =
new StreamWriter(stream);
var view = this.GetViewInstance(viewLocationResult, renderContext, referencingAssembly, model);
view.ExecuteView(null, null);
var body = view.Body;
var sectionContents = view.SectionContents;
var layout = view.HasLayout ? view.Layout : GetViewStartLayout(model, renderContext, referencingAssembly, isPartial);
var root = string.IsNullOrWhiteSpace(layout);
while (!root)
{
view = this.GetViewInstance(renderContext.LocateView(layout, model), renderContext, referencingAssembly, model);
view.ExecuteView(body, sectionContents);
body = view.Body;
sectionContents = view.SectionContents;
layout = view.HasLayout ? view.Layout : GetViewStartLayout(model, renderContext, referencingAssembly, isPartial);
root = !view.HasLayout;
}
writer.Write(body);
writer.Flush();
};
return response;
}
示例2: GetViewStartLayout
private string GetViewStartLayout(dynamic model, IRenderContext renderContext, Assembly referencingAssembly, bool isPartial)
{
if (isPartial)
{
return string.Empty;
}
var view = renderContext.LocateView("_ViewStart", model);
if (view == null)
{
return string.Empty;
}
if (!this.Extensions.Any(x => x.Equals(view.Extension, StringComparison.OrdinalIgnoreCase)))
{
return string.Empty;
}
var viewInstance = GetViewInstance(view, renderContext, referencingAssembly, model);
viewInstance.ExecuteView(null, null);
return viewInstance.Layout ?? string.Empty;
}
示例3: RenderView
/// <summary>
/// Renders the view.
/// </summary>
/// <param name="viewLocationResult">A <see cref="ViewLocationResult"/> instance, containing information on how to get the view template.</param>
/// <param name="model">The model that should be passed into the view</param>
/// <param name="renderContext">The render context.</param>
/// <param name="isPartial">Used by HtmlHelpers to declare a view as partial</param>
/// <returns>A response.</returns>
public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext, bool isPartial)
{
Assembly referencingAssembly = null;
var response = new HtmlResponse();
response.Contents = stream =>
{
var writer =
new StreamWriter(stream);
var view = this.GetViewInstance(viewLocationResult, renderContext, model);
view.ExecuteView(null, null);
var body = view.Body;
var sectionContents = view.SectionContents;
var layout = view.HasLayout ? view.Layout : GetViewStartLayout(model, renderContext, referencingAssembly, isPartial);
var root = string.IsNullOrWhiteSpace(layout);
while (!root)
{
var viewLocation =
renderContext.LocateView(layout, model);
if (viewLocation == null)
{
throw new InvalidOperationException("Unable to locate layout: " + layout);
}
view = this.GetViewInstance(viewLocation, renderContext, model);
view.ExecuteView(body, sectionContents);
body = view.Body;
sectionContents = view.SectionContents;
layout = view.HasLayout ? view.Layout : GetViewStartLayout(model, renderContext, referencingAssembly, isPartial);
root = !view.HasLayout;
}
writer.Write(body);
writer.Flush();
};
return response;
}