使用ViewRenderer在SignalR响应中渲染部分视图?

时间:2014-05-26 12:02:56

标签: c# asp.net-mvc-4 signalr render partial-views

我将在SignalR类(Hub)中呈现局部视图 所以,我在BaseHub类中创建了一个方法:

protected static string RenderPartialToString(string view, object model)
{
    // Create an arbitrary controller instance
    EmptyController controller = ViewRenderer.CreateController<EmptyController>();

    // view = "~/Views/Partials/UploadedFilePartial.cshtml"
    // model = List<AttachmentModel>
    string html = ViewRenderer.RenderPartialView(view, model, controller.ControllerContext);
    return html;
}

但是在ViewRenderer的下一行我得到了一个例外:

// first find the ViewEngine for this view
ViewEngineResult viewEngineResult = partial
    ? ViewEngines.Engines.FindPartialView(Context, viewPath)
    : ViewEngines.Engines.FindView(Context, viewPath, null);

例外: Response is not available in this context.
堆栈跟踪:

at System.Web.HttpContext.get_Response()
at System.Web.HttpContextWrapper.get_Response()
at System.Web.WebPages.CookieBrowserOverrideStore.GetOverriddenUserAgent(HttpContextBase httpContext)
at System.Web.WebPages.BrowserHelpers.GetOverriddenUserAgent(HttpContextBase httpContext)
at System.Web.WebPages.BrowserHelpers.GetOverriddenBrowser(HttpContextBase httpContext, Func`2 createBrowser)
at System.Web.WebPages.BrowserHelpers.GetOverriddenBrowser(HttpContextBase httpContext)
at System.Web.WebPages.DisplayModeProvider.<.ctor>b__2(HttpContextBase context)
at System.Web.WebPages.DefaultDisplayMode.CanHandleContext(HttpContextBase httpContext)
at System.Web.WebPages.DisplayModeProvider.<GetAvailableDisplayModesForContext>d__4.MoveNext()
at System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[] locations, String[] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations)
at System.Web.Mvc.VirtualPathProviderViewEngine.FindPartialView(ControllerContext controllerContext, String partialViewName, Boolean useCache)
at System.Web.Mvc.ViewEngineCollection.<>c__DisplayClass2.<FindPartialView>b__0(IViewEngine e)
at System.Web.Mvc.ViewEngineCollection.Find(Func`2 lookup, Boolean trackSearchedPaths)
at System.Web.Mvc.ViewEngineCollection.Find(Func`2 cacheLocator, Func`2 locator)
at System.Web.Mvc.ViewEngineCollection.FindPartialView(ControllerContext controllerContext, String partialViewName)
at Smartiz.Admin.ViewRenderer.RenderViewToStringInternal(String viewPath, Object model, Boolean partial) in d:\My Works\C Sharp\Smartiz\Smartiz v2.0\Smartiz.Admin\Classes\ViewRenderer.cs:line 129
at Smartiz.Admin.ViewRenderer.RenderPartialViewToString(String viewPath, Object model) in d:\My Works\C Sharp\Smartiz\Smartiz v2.0\Smartiz.Admin\Classes\ViewRenderer.cs:line 74
at Smartiz.Admin.ViewRenderer.RenderPartialView(String viewPath, Object model, ControllerContext controllerContext) in d:\My Works\C Sharp\Smartiz\Smartiz v2.0\Smartiz.Admin\Classes\ViewRenderer.cs:line 110
at Smartiz.Admin.BaseHub.RenderPartialToString(String view, Object model) in d:\My Works\C Sharp\Smartiz\Smartiz v2.0\Smartiz.Admin\SignalR\BaseHub.cs:line 55
at Smartiz.Admin.FileLeecherHub.GetFileFromWeb(String fileUrl, Int32 deviceId, Int32 wikiId, Int32 contentId) in d:\My Works\C Sharp\Smartiz\Smartiz v2.0\Smartiz.Admin\SignalR\FileLeecherHub.cs:line 102

P.S:
我在以下链接中阅读了ViewRenderer http://www.codemag.com/Article/1312081
您可以在以下链接中找到ViewRendererhttps://github.com/RickStrahl/WestwindToolkit/blob/master/Westwind.Web.Mvc/Utils/ViewRenderer.cs

1 个答案:

答案 0 :(得分:1)

我终于找到了解决方案:
在我的部分视图中,我使用了Html.TextBoxUrl.Content。我认为问题与他们有关 经过多次在Google搜索后,我找到了RazorEngine,我可以通过以下方式解决我的问题:

protected static string RenderPartialToString(string view, object model)
{
    string partialViewPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, view.Replace("~/", ""));
    string template = File.ReadAllText(partialViewPath);
    DynamicViewBag viewBag = new DynamicViewBag();
    viewBag.AddValue("UploadFileFromUrl", true);
    string html = Razor.Parse(template, model, viewBag, null);

    return html;
}

<强> P.S:
我在部分视图中替换了Html.TextBoxUrl.Content。因为RazorEngine有问题。