Where is the documentation of System.Web.Razor? (v2) [closed]

让人想犯罪 __ 提交于 2019-12-06 19:50:30

I left this question open for almost a year without an answer, so I decided to post what I came up with eventually.

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.

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.

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.

Here is the 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;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!