问题描述
我已经得到了来自的NuGet剃刀发动机的V2版本。我想用它的API编写的即时剃刀意见。然而,这似乎是完全没有证件。
I've got the v2 version of the Razor engine from NuGet. I'd like to compile Razor views on-the-fly using its API. However, it seems to be fully non-documented.
每一个类型,成员有下列文件:
Every single type and member has the following documentation:
这类型/成员支持.NET Framework基础结构,是 不打算直接从您的code使用。
这是很讨厌的,因为这些都是公共类型和公共成员。
我见过一些第三方的东西,这确实编译剃刀认为使用这个库,所以我知道这个任务应该是可行的了。
This is very irritating, since these are all public types and public members.
I've seen some 3rd party stuff which does compile Razor views using this library, so I know that the task should be doable too.
那么,有没有关于如何在任何地方使用这个API的实际可用的文档?
So, is there any actual usable documentation on how to use this API anywhere?
推荐答案
我离开这个问题打开了近一年没有答案,所以我决定发布什么,我想出了最后。
I left this question open for almost a year without an answer, so I decided to post what I came up with eventually.
这是pretty的清楚,剃刀仍然无证,看到的,我认为这是很可能仍然无证。
It is pretty clear that Razor is still undocumented, see http://msdn.microsoft.com/en-us/library/system.web.razor%28v=vs.111%29.aspx and I think it is very likely to remain undocumented.
但是,如何使用它可以很容易地通过查看ASP.NET MVC如何使用它的剃刀视图引擎的code确定。然后,您可以编写基于该code。
However, how to use it can be easily determined by looking at the code of how ASP.NET MVC uses it in its Razor view engine. You can then write code based on that.
看来,剃须刀也关系到ASP.NET的 BuildManager
的基础设施,所以你可以很容易地得到一个剃刀的一个实例查看通过。然后,你正在寻找调用 ExecutePageHierarchy
方法。
It seems that Razor also ties into the ASP.NET BuildManager
infrastructure, so you can easily get an instance of a Razor view through that. Then, you are looking for calling the ExecutePageHierarchy
method.
下面是code:
public void ProcessRequestCore(HttpContextBase context)
{
try
{
// Create Razor page instance
var instance = BuildManager.CreateInstanceFromVirtualPath(_razorFilePath, typeof(WebPage)) as WebPage;
if (instance == null)
throw new NullReferenceException("BuildManager.CreateInstanceFromVirtualPath returned null.");
// Set up things
instance.VirtualPath = _virtualPath;
// Render the Razor page
instance.ExecutePageHierarchy(new WebPageContext(context, instance, _model), context.Response.Output);
}
catch (Exception exc)
{
Logger.WriteException(exc);
context.Response.StatusCode = 500;
}
}
这篇关于哪里是System.Web.Razor的文件? (2版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!