我在MVC中使用WCF服务,当WCF服务的某些方法调用时,我需要WCF服务中的部分视图HTML。价值不符合预期范围内的MVC与WCF服务
我在我的WCF服务中使用下面的代码来呈现HTML,但无法成功,因为我在'viewResult.View.Render'行中出现'值不在预期范围内'的错误。
public static string RenderRazorViewToString(string viewPath, object model, HttpContextBase context, Guid? tenantId)
{
try
{
using (var controller = new BaseController())
{
if (context == null)
{
Logger.Error(
"RenderRazorViewToString context is null.Empty string returned.",
new Exception("context is null."));
return string.Empty;
}
var routeData = new RouteData();
routeData.Values.Add("controller", "Base");
controller.ControllerContext = new ControllerContext(context, routeData, controller);
controller.ViewBag.TenantId = tenantId;
return controller.RenderRazorViewToString(viewPath, model);
}
}
catch (Exception ex)
{
Logger.Error("RenderRazorViewToString", ex);
return string.Empty;
}
}
public static string RenderRazorViewToString(this Controller controller, string viewPath, object model)
{
try
{
BrowserOverrideStores.Current = new CustomBrowserOverrideStore();
controller.ViewData.Model = model;
using (var stringWriter = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewPath);
var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, stringWriter);
viewResult.View.Render(viewContext, stringWriter);
viewResult.ViewEngine.ReleaseView(controller.ControllerContext, viewResult.View);
return stringWriter.GetStringBuilder().ToString();
}
}
catch (Exception ex)
{
Logger.Error("RenderRazorViewToString Extension Method", ex);
return string.Empty;
}
}
我打电话像上面这样的方法:
var result = RenderRazorViewToString(@"~/Views/RealTimeView/BaysContainers/_BayEntry.cshtml", realTimeInfo, new HttpContextWrapper(System.Web.HttpContext.Current))
管窥
@using CloudAmber.DSMS.DataClasses
@model CloudAmber.DSMS.DataClasses.RealTimeBayInfo
<div>
@if (Model.Fleets != null)
{
foreach (var item in Model.Fleets)
{
<li id="@[email protected]@item.DutyBoardNumber" @(item.IsAllocated ? "allocatedbus" : "unallocatedbuses") data-bayidcontainer="@Model.BayID" @(item.IsAllocated ? FiltersType.CirculatingBuses.ToString() : "")>
<span class="IconDiv">
<a style="cursor: pointer;" id="[email protected]()" data-fleetnumber="@item.FleetNumber" data-vehiclebase="true" data-bayid="@Model.BayID" data-bayidentifier="@Model.BayIdentifier" data-baycode="@Model.BayCode"
data-operatorcode="@item.OperatorCode" data-ismanagebus="true">
@{
var color = item.IsAllocated ? "#000" : "#f00";
}
<i class="fa fa-bus" style="font-size:20px;color:@color"></i>
</a>
</span>
@{
string articulatedBus = string.Empty;
if (item.VehicleType == VehicleType.Articulated)
{
articulatedBus = MessageResource.ArticulatedFleetPostfix;
}
}
<span class="BusNo" title="@Resource.FleetNo - @Resource.PublicServiceCode">
@Html.GetValueOrNa(item.FleetNumber) - @Html.GetValueOrNa(item.PublicServiceCode) @articulatedBus
</span>
</li>
}
}
<input type="hidden" id="[email protected]" value="@(Model.PredictedDepartureDateTime.HasValue ? Model.PredictedDepartureDateTime.Value.ToString("o") : string.Empty)" />
<input type="hidden" id="[email protected]" value="@(Model.ScheduledDepartureDateTime.HasValue ? Model.ScheduledDepartureDateTime.Value.ToString("o") : string.Empty)" />
</div>
我很困惑你的问题:使用wcf服务来提供html文件,特别是使用剃刀? –
它必须在View代码的某个地方爆炸。你可以调试它,并寻找一个内部异常,可能会给你一个线索在哪里? –
没有内在的例外。是的,我使用WCF服务来提供html文件。 – PrinceT