简体   繁体   English

MVC渲染视图到字符串

[英]MVC Render View to String

I have found a few other questions like this, however every single one has not been able to fix this problem I am having. 我发现了一些像这样的问题,但是每一个问题我都无法解决这个问题。 I continuously get the same problem. 我不断遇到同样的问题。

I have checked out this and that and multiple others! 我已经检查过这个那个以及其他多个人! Every single thing I get the same problem. 每件事我都会遇到同样的问题。

I get the following exception: 我得到以下异常:

The view at '~/Views/Email/Ticket.cshtml' must derive from WebViewPage, or WebViewPage<TModel>.

The code I am using is: 我使用的代码是:

public static string RenderViewToString(string controllerName, string viewName, object viewModel)
{
    HttpContextWrapper context = new HttpContextWrapper(HttpContext.Current);
    RouteData routeData = new System.Web.Routing.RouteData();
    routeData.Values.Add("controller", controllerName);
    ControllerContext controllerContext = new ControllerContext(context, routeData, new RenderController());


    RazorViewEngine razorViewEngine = new RazorViewEngine();
    ViewEngineResult razorViewResult = razorViewEngine.FindView(controllerContext, viewName, string.Empty, false);


    using (StringWriter writer = new StringWriter())
    {
        ViewDataDictionary viewData = new ViewDataDictionary(viewModel);
        var viewContext = new ViewContext(controllerContext, razorViewResult.View, viewData, new TempDataDictionary(), writer);
        razorViewResult.View.Render(viewContext, writer);
        writer.Flush();
        return writer.GetStringBuilder().ToString();
    }
}

The RenderController is just: RenderController只是:

private class RenderController : ControllerBase
{
    protected override void ExecuteCore()
    {
    }
}

No matter what I have done, or different methods I have tried nothing seems to make this error go away, I have tried making the view inherit the objects but you can't have @model and @inherit in the same thing. 无论我做了什么,或者我尝试过的不同方法似乎都没有让这个错误消失,我试图让视图继承对象,但是你不能在同一个东西中使用@model和@inherit。 Getting quite aggravated over this as I have followed pretty much everyone's instructions on other questions/posts but hasn't been able to help me. 由于我几乎遵循了所有人对其他问题/帖子的指示但却未能帮助我,因此对此非常加重。

Thanks to @JustinRusso he helped figure this out! 感谢@JustinRusso,他帮助解决了这个问题!

What seemed to happen was, my refernce to MVC 5.0.0.0 in my actual web project was different to my MVC version that I was referencing in my Common library which was at 4.0.0.1. 似乎发生了什么,我在我的实际web项目中对MVC 5.0.0.0的引用与我在我的Common库中引用的MVC版本不同,该版本是4.0.0.1。

After I updated the version it worked. 我更新了它的版本后。 I feel stupid now looking back on it but that is what learning is all about! 我现在回想起来感觉很愚蠢,但这就是学习的全部!

Thanks to all for the help and suggestions! 感谢大家的帮助和建议!

I don't think you can mark comments as answers (at least from what I can tell) so just making it a bigger item so future people can find it. 我不认为你可以将评论标记为答案(至少从我能说的内容),所以只需将它作为一个更大的项目,以便未来的人们可以找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM