本文整理汇总了C#中ViewLocationResult类的典型用法代码示例。如果您正苦于以下问题:C# ViewLocationResult类的具体用法?C# ViewLocationResult怎么用?C# ViewLocationResult使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ViewLocationResult类属于命名空间,在下文中一共展示了ViewLocationResult类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RenderView
public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext)
{
var template = renderContext.ViewCache.GetOrAdd(viewLocationResult, result =>
{
try
{
var context = new NancyVeilContext(renderContext, Extensions);
var engine = new VeilEngine(context);
Type modelType = model == null ? typeof(object) : model.GetType();
return engine.CompileNonGeneric(viewLocationResult.Extension, result.Contents(), modelType);
}
catch (Exception e)
{
return CreateErrorPage(e);
}
});
var response = new HtmlResponse();
response.ContentType = "text/html; charset=utf-8";
response.Contents = s =>
{
var writer = new StreamWriter(s, Encoding.UTF8);
template(writer, model);
writer.Flush();
};
return response;
}
示例2: Include_should_look_for_a_partial
public void Include_should_look_for_a_partial()
{
// Set up the view startup context
string partialPath = Path.Combine(Environment.CurrentDirectory, @"TestViews\_partial.liquid");
// Set up a ViewLocationResult that the test can use
var testLocation = new ViewLocationResult(
Environment.CurrentDirectory,
"test",
"liquid",
() => new StringReader(@"<h1>Including a partial</h1>{% include 'partial' %}")
);
var partialLocation = new ViewLocationResult(
partialPath,
"partial",
"liquid",
() => new StringReader(File.ReadAllText(partialPath))
);
var currentStartupContext = CreateContext(new [] {testLocation, partialLocation});
this.engine = new DotLiquidViewEngine(new LiquidNancyFileSystem(currentStartupContext));
// Given
var stream = new MemoryStream();
// When
var response = this.engine.RenderView(testLocation, null, this.renderContext);
response.Contents.Invoke(stream);
// Then
stream.ShouldEqual("<h1>Including a partial</h1>Some template.");
}
示例3: GetTemplateFromFile
public static Template GetTemplateFromFile(ViewLocationResult path)
{
// can't cache anything here since template depends on current context
var contents = path.Contents;
var template = Template.Parse(contents);
return template;
}
示例4: 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"></param>
/// <returns>A delegate that can be invoked with the <see cref="Stream"/> that the view should be rendered to.</returns>
public Action<Stream> RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext)
{
return stream =>{
var templateManagerProvider =
new TemplateManagerProvider()
.WithLoader(new TemplateLoader(viewLocationResult.Contents.Invoke()));
var templateManager =
templateManagerProvider.GetNewManager();
var template = renderContext.ViewCache.GetOrAdd(
viewLocationResult,
x => templateManager.GetTemplate(string.Empty));
var context = new Dictionary<string, object> { { "Model", model } };
var reader = template.Walk(templateManager, context);
var writer =
new StreamWriter(stream);
writer.Write(reader.ReadToEnd());
writer.Flush();
};
}
示例5: Should_be_able_to_render_view_with_partial_to_stream
public void Should_be_able_to_render_view_with_partial_to_stream()
{
// Given
var location = new ViewLocationResult(
string.Empty,
string.Empty,
"cshtml",
() => new StringReader(@"@{var x = ""test"";}<h1>Hello Mr. @x</h1> @Html.Partial(""partial.cshtml"")")
);
var partialLocation = new ViewLocationResult(
string.Empty,
"partial.cshtml",
"cshtml",
() => new StringReader(@"this is partial")
);
A.CallTo(() => this.renderContext.LocateView("partial.cshtml",null)).Returns(partialLocation);
var stream = new MemoryStream();
// When
var response = this.engine.RenderView(location, null,this.renderContext);
response.Contents.Invoke(stream);
// Then
stream.ShouldEqual("<h1>Hello Mr. test</h1> this is partial");
}
示例6: Should_return_Markdown
public void Should_return_Markdown()
{
// Given
const string markdown = @"#Header1
##Header2
###Header3
Hi there!
> This is a blockquote.";
var location = new ViewLocationResult(
string.Empty,
string.Empty,
"md",
() => new StringReader(markdown)
);
var html = this.viewEngine.ConvertMarkdown(location);
var stream = new MemoryStream();
A.CallTo(() => this.renderContext.ViewCache.GetOrAdd(location, A<Func<ViewLocationResult, string>>.Ignored))
.Returns(html);
// When
var response = this.viewEngine.RenderView(location, null, this.renderContext);
response.Contents.Invoke(stream);
// Then
var result = ReadAll(stream);
result.ShouldEqual(html);
}
示例7: GetExtraParameters
public virtual IDictionary<string, object> GetExtraParameters(ViewLocationResult viewLocationResult)
{
var extra = new Dictionary<string, object>();
foreach (var filter in Filters)
{
filter.ExtraParameters(viewLocationResult, extra);
}
return extra;
}
示例8: 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>
/// <returns>A response.</returns>
public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext)
{
return new HtmlResponse(contents: s =>
{
var writer = new StreamWriter(s);
var templateContents = renderContext.ViewCache.GetOrAdd(viewLocationResult, vr => vr.Contents.Invoke().ReadToEnd());
writer.Write(this.viewEngine.Render(templateContents, model, new NancyViewEngineHost(renderContext)));
writer.Flush();
});
}
示例9: RenderView
public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext)
{
System.Diagnostics.Debug.WriteLine("test");
return new HtmlResponse(
contents: stream =>
{
var templateFactory = renderContext.ViewCache.GetOrAdd(
viewLocationResult, x => GetTemplateFactory(viewLocationResult));
RenderTemplateFromTemplateFactory(templateFactory, stream, model, renderContext.Context);
}
);
}
示例10: 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>
/// <returns>A delegate that can be invoked with the <see cref="Stream"/> that the view should be rendered to.</returns>
public Action<Stream> RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext)
{
Interlocked.CompareExchange(ref this.viewEngine, new SuperSimpleViewEngine(new NancyViewEngineHost(renderContext)), null);
return s =>
{
var writer = new StreamWriter(s);
var templateContents = renderContext.ViewCache.GetOrAdd(viewLocationResult, vr => vr.Contents.Invoke().ReadToEnd());
writer.Write(this.viewEngine.Render(templateContents, model));
writer.Flush();
};
}
示例11: ConvertMarkdown
/// <summary>
/// Converts the markdown.
/// </summary>
/// <returns>
/// HTML converted from markdown
/// </returns>
/// <param name='viewLocationResult'>
/// View location result.
/// </param>
public string ConvertMarkdown(ViewLocationResult viewLocationResult)
{
string content =
viewLocationResult.Contents().ReadToEnd();
if (content.StartsWith("<!DOCTYPE html>"))
{
return MarkdownViewengineRender.RenderMasterPage(content);
}
var parser = new Markdown();
var html = parser.Transform(content);
return ParagraphSubstitution.Replace(html, "$1");
}
示例12: 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 to be passed into the view</param>
/// <param name="renderContext"></param>
/// <returns>A response</returns>
public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext)
{
return new HtmlResponse
{
Contents = stream =>
{
var templateName = viewLocationResult.Name;
var handlebars = threadLocalHandlebars.Value;
handlebars.RegisterTemplate(templateName, () =>
{
using (var textReader = viewLocationResult.Contents())
{
return textReader.ReadToEnd();
}
});
foreach (var partial in viewLocator.GetAllCurrentlyDiscoveredViews().Where(x => x.Name.StartsWith("_")))
{
var partialName = partial.Name.TrimStart('_');
handlebars.RegisterPartial(partialName, () =>
{
using (var textReader = partial.Contents())
{
return textReader.ReadToEnd();
}
});
}
using (var writer = new StreamWriter(stream))
{
dynamic output;
try
{
output = handlebars.Transform(templateName, model);
}
catch (Exception)
{
//TODO: remove this exception handling after a few versions
var templateContents = viewLocationResult.Contents().ReadToEnd();
if (templateContents.Contains("{{> _") || templateContents.Contains("{{>_"))
{
throw new Exception(string.Format("Template '{0}' contains and underscore prefixed partial name. This is no longer required. Search for the string '{{>_' or '{{> _' in your template and remove the '_'.", templateName));
}
throw;
}
writer.Write(output);
}
}
};
}
示例13: RenderViewSetsPath
public void RenderViewSetsPath()
{
// arrange
var templateLocator = A.Fake<IViewLocator>();
var viewCompiler = A.Fake<IViewCompiler>();
var viewLocationResult = new ViewLocationResult(@"c:\some\fake\path", null);
A.CallTo(() => templateLocator.GetTemplateContents("test")).Returns(viewLocationResult);
var engine = new RazorViewEngine(templateLocator, viewCompiler);
// act
var result = engine.RenderView("test", null);
// assert
result.Location.ShouldEqual(@"c:\some\fake\path");
}
示例14: Render_should_create_valid_nhaml_IViewSource
public void Render_should_create_valid_nhaml_IViewSource()
{
// Given
var textReader = new StringReader("Hello world");
var viewLocationResult = new ViewLocationResult("folder", "file", "haml", () => textReader);
// When
var template = engine.RenderView(viewLocationResult, null, this.renderContext);
template.Contents.Invoke(new MemoryStream());
// Then
A.CallTo(() => nHamlEngine.GetCompiledTemplate(
A<NancyNHamlView>.That.Matches(x => x.FilePath == @"folder\file.haml" && x.GetTextReader() == textReader),
A<Type>.Ignored)).MustHaveHappened();
}
示例15: RenderView
public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext)
{
//we already have a view but we still need a view provider
return new HtmlResponse(contents: stream =>
{
var host = new NancyParrotHost(_modelValueProviderFactory, new NancyPathResolver(renderContext));
var parrotWriter = host.CreateWriter();
var rendererFactory = host.RendererFactory;
var view = new ParrotView(host, rendererFactory, _parrotViewLocator, viewLocationResult.Contents, parrotWriter);
var writer = new StreamWriter(stream);
view.Render(model, writer);
writer.Flush();
});
}