如何通过它的完整文件位置获取任何视图的Html [英] How to get Html of any view by it's full file location

查看:63
本文介绍了如何通过它的完整文件位置获取任何视图的Html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public string GetRazorViewAsString(string filePath, object model = null)
{
        var resultString = new StringWriter();

        var context = new HttpContextWrapper(HttpContext.Current);
        var routeData = new RouteData();

        // Creating the controller context
        var controllerContext = new ControllerContext(new RequestContext(context, routeData), new DummyController());

        // Rendering the view and getting the html to resultString
        var razor = new RazorView(controllerContext, filePath, null, false, null);
        razor.Render(new ViewContext(controllerContext, razor, new ViewDataDictionary(model), new TempDataDictionary(), resultString), resultString);

        // Returning the generated html
        return resultString.ToString();
 }

public class DummyController : Controller { }





目前,我们使用上面的代码为视图生成HTML。在那里,视图路径是一个虚拟路径。



现在,我们计划将视图移到项目之外。因此,现在无法保持虚拟路径。



通过获取视图的完整路径来获取HTML的任何帮助。



Currently, we are using above code for generating HTML for a view. In that, view path is a virtual path.

Now, we are planning to move the views outside of the project. So keeping virtual path is not possible now.

Any help on getting HTML by taking the full path of the view.

推荐答案

你不能。视图必须在项目内。代码将无法访问其运行的文件夹之外的任何内容。
You can't. The views must be inside the project. The code will not have access to anything outside of the folder it's running from.


这篇关于如何通过它的完整文件位置获取任何视图的Html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆