我需要能够通过 signalR 在我的 MVC 应用程序中将部分视图作为字符串返回。我正在使用集线器。
我正在使用以下方法返回部分视图字符串(从这里):
public static string RenderPartialView(string controllerName, string partialView, object model)
{
var context = httpContextBase as HttpContextBase;
var routes = new RouteData();
routes.Values.Add("controller", controllerName);
var requestContext = new RequestContext(context, routes);
string requiredString = requestContext.RouteData.GetRequiredString("controller");
var controllerFactory = ControllerBuilder.Current.GetControllerFactory();
var controller = controllerFactory.CreateController(requestContext, requiredString) as ControllerBase;
controller.ControllerContext = new ControllerContext(context, routes, controller);
var ViewData = new ViewDataDictionary();
var TempData = new TempDataDictionary();
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, partialView);
var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
因此,为了使此方法起作用,我需要HttpContext.Current
在我的 OnConnected 中(我注意到它始终存在)我将其设置为:
public class TaskActionStatus : Hub
{
private static HttpContextBase httpContextBase;
...
public override Task OnConnected()
{
httpContextBase = new HttpContextWrapper(HttpContext.Current) as HttpContextBase;
...
然后我在我的 RenderPartialView 方法中使用它:
var context = httpContextBase as HttpContextBase;
这样我总是可以访问当前的 HttpContext。但是,我注意到有时我的 HttpContext 静态副本为空。这是为什么?。
- 这里最好的方法是什么?
- 有没有办法在没有 HttpContext 的情况下呈现局部视图?