c# - 使用 ViewRenderer 在 SignalR 响应中渲染 PartialView?

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

我要在 SignalR 类(Hub)中渲染部分 View
因此,我在 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);

异常(exception): 在此上下文中无法提供响应。
StackTrace:

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

附注:
我在以下链接中阅读了有关 ViewRenderer 的内容
http://www.codemag.com/Article/1312081
您可以在以下链接中找到ViewRenderer: https://github.com/RickStrahl/WestwindToolkit/blob/master/Westwind.Web.Mvc/Utils/ViewRenderer.cs

最佳答案

我终于找到了解决方案:
在我的部分 View 中,我使用了 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;
}

附注:
我在部分 View 中替换了 Html.TextBoxUrl.Content 。因为 RazorEngine 有问题。

关于c# - 使用 ViewRenderer 在 SignalR 响应中渲染 PartialView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23869725/

相关文章:

c# - 如何扩展 asp.net User.Identity 属性?

c# - WPF 中的自定义附加事件

c# - WCF 推荐序列化多个对象的方法

javascript - 从未在 ApiController 中调用 Post 方法

c# - 如何在 vNext 中获取 SignalR 端点列表?

c# - 捕获组的一部分

asp.net - 在 View 中使用 RenderPartial 时不显示 ValidationSummary()

c# - mvc 4 - Jquery mobile - @section 脚本在导航后不起作用

mono - Nginx、SignalR 和带有单声道的 FastCGI : No application found

signalr - 组订阅是否会在重新连接时自动处理?