gpt4 book ai didi

c# - 使用 ViewRenderer 在 SignalR 响应中呈现 PartialView?

转载 作者:太空宇宙 更新时间:2023-11-03 13:21:51 29 4
gpt4 key购买 nike

我将在 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);

异常:响应在此上下文中不可用。
堆栈跟踪:

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。我认为问题与他们有关。
在谷歌多次搜索后,我找到了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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com