I have an asp.net core application and I have this service called ViewRenderService that converts a view to string for me to render certain views based on the request being made.
ViewRenderService:
public class ViewRenderService : IViewRenderService
{
private readonly IRazorViewEngine _razorViewEngine;
private readonly ITempDataProvider _tempDataProvider;
private readonly IServiceProvider _serviceProvider;
public ViewRenderService(IRazorViewEngine razorViewEngine,
ITempDataProvider tempDataProvider,
IServiceProvider serviceProvider)
{
_razorViewEngine = razorViewEngine;
_tempDataProvider = tempDataProvider;
_serviceProvider = serviceProvider;
}
public async Task<string> RenderToStringAsync(string viewName, object model)
{
var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider };
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
using (var sw = new StringWriter())
{
var viewResult = _razorViewEngine.GetView(viewName, viewName, false);
if (viewResult.View == null)
{
throw new ArgumentNullException($"{viewName} does not match any available view");
}
var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
{
Model = model
};
var viewContext = new ViewContext(
actionContext,
viewResult.View,
viewDictionary,
new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
sw,
new HtmlHelperOptions()
);
await viewResult.View.RenderAsync(viewContext);
return sw.GetStringBuilder().ToString();
}
}
}
The line:
await viewResult.View.RenderAsync
throws
"System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index"
on certain Model/Views
Views and Model it throws exception on:
VerifyCodeViewModel:
public class VerifyCodeViewModel
{
[Required (ErrorMessage = "Please enter verification code that was sent via email")]
public string Code { get; set; }
public string ReturnUrl { get; set; }
}
Verify.cshtml
@model VerifyCodeViewModel
<div class="row">
<div class="col s12 m5 l5 push-m3 push-l3 center">
<div class="center">
<br />
<form method="post" asp-action="Verify">
<div class="card">
<div class="card-content">
<div class="red-text text-bold" asp-validation-summary="ModelOnly"></div>
<div class="row">
<div class="col 12 m12 l12">
<h4 class="text-bold header caption-uppercase">Verification Token Required</h4>
</div>
</div>
<div class="row">
<div class="col s12 m12 l12">
<p class="center-align">
An email that contains a token have been sent to you. Please enter token below.
</p>
</div>
</div>
<div class="row">
<div class="input-field col 12 m12 l12">
<i class="mdi-communication-vpn-key prefix"></i>
<input type="text" asp-for="Code" placeholder="Enter token here"/>
<span asp-validation-for="Code"></span>
<span class="text-bold red-text">@ViewBag.Message</span>
</div>
</div>
</div>
<div class="card-action">
<button type="submit" class="btn btn-danger">Verify</button>
</div>
</div>
</form>
</div>
</div>
</div>
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}
Guide me as to what I am missing please as it does work for some views but not the one above.
I got the same problem and found the answer by inspecting my call-stack. The problem on my side was :
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at System.Collections.Generic.List`1.get_Item(Int32 index)
at Microsoft.AspNetCore.Mvc.Routing.UrlHelper.get_Router()
at Microsoft.AspNetCore.Mvc.Routing.UrlHelper.GetVirtualPathData(String routeName, RouteValueDictionary values)
at Microsoft.AspNetCore.Mvc.Routing.UrlHelper.Action(UrlActionContext actionContext)
So the Router was missing and this problem was fixed by changing the code to the following:
public class ViewRenderService : IViewRenderService
{
private readonly IRazorViewEngine _razorViewEngine;
private readonly ITempDataProvider _tempDataProvider;
private readonly IServiceProvider _serviceProvider;
private readonly HttpContext _context;
public ViewRenderService(IRazorViewEngine razorViewEngine,
ITempDataProvider tempDataProvider,
IServiceProvider serviceProvider,
IHttpContextAccessor accessor)
{
_razorViewEngine = razorViewEngine;
_tempDataProvider = tempDataProvider;
_serviceProvider = serviceProvider;
_context = accessor.HttpContext;
}
public async Task<string> RenderToStringAsync(string viewName, object model)
{
var actionContext = new ActionContext(_context, new RouteData(), new ActionDescriptor());
using (var sw = new StringWriter())
{
var viewResult = _razorViewEngine.GetView(viewName, viewName, false);
if (viewResult.View == null)
{
throw new ArgumentNullException($"{viewName} does not match any available view");
}
var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
{
Model = model
};
var viewContext = new ViewContext(
actionContext,
viewResult.View,
viewDictionary,
new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
sw,
new HtmlHelperOptions()
);
viewContext.RouteData = _context.GetRouteData();
await viewResult.View.RenderAsync(viewContext);
return sw.GetStringBuilder().ToString();
}
}
}
What actually helped was changing the context from a new ActionContext to a injected one. By changing this and then setting the RouteData the problem was fixed.
viewContext.RouteData = _context.GetRouteData();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With