Created
May 18, 2016 10:17
-
-
Save ahmad-moussawi/1643d703c11699a6a4046e57247b4d09 to your computer and use it in GitHub Desktop.
A view Render for Razor (aspnetcore RC2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeController : Controller { | |
private readonly ViewRender view; | |
public HomeController (ViewRender view) { | |
this.view = view | |
} | |
public string Test () { | |
// render ~/Views/Emails/ResetCode | |
var html = this.view.Render("Emails/ResetCode", new ResetCodeModel { Code : "123456" }); | |
return html; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Startup { | |
// .... | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
// .... | |
services.AddScoped<ViewRender, ViewRender>(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.AspNetCore.Mvc.Abstractions; | |
using Microsoft.AspNetCore.Mvc.ModelBinding; | |
using Microsoft.AspNetCore.Mvc.Razor; | |
using Microsoft.AspNetCore.Mvc.Rendering; | |
using Microsoft.AspNetCore.Mvc.ViewFeatures; | |
using Microsoft.AspNetCore.Routing; | |
namespace Forecast.Services | |
{ | |
public class ViewRender | |
{ | |
private IRazorViewEngine _viewEngine; | |
private ITempDataProvider _tempDataProvider; | |
private IServiceProvider _serviceProvider; | |
public ViewRender( | |
IRazorViewEngine viewEngine, | |
ITempDataProvider tempDataProvider, | |
IServiceProvider serviceProvider) | |
{ | |
_viewEngine = viewEngine; | |
_tempDataProvider = tempDataProvider; | |
_serviceProvider = serviceProvider; | |
} | |
public string Render<TModel>(string name, TModel model) | |
{ | |
var actionContext = GetActionContext(); | |
var viewEngineResult = _viewEngine.FindView(actionContext, name, false); | |
if (!viewEngineResult.Success) | |
{ | |
throw new InvalidOperationException(string.Format("Couldn't find view '{0}'", name)); | |
} | |
var view = viewEngineResult.View; | |
using (var output = new StringWriter()) | |
{ | |
var viewContext = new ViewContext( | |
actionContext, | |
view, | |
new ViewDataDictionary<TModel>( | |
metadataProvider: new EmptyModelMetadataProvider(), | |
modelState: new ModelStateDictionary()) | |
{ | |
Model = model | |
}, | |
new TempDataDictionary( | |
actionContext.HttpContext, | |
_tempDataProvider), | |
output, | |
new HtmlHelperOptions()); | |
view.RenderAsync(viewContext).GetAwaiter().GetResult(); | |
return output.ToString(); | |
} | |
} | |
private ActionContext GetActionContext() | |
{ | |
var httpContext = new DefaultHttpContext(); | |
httpContext.RequestServices = _serviceProvider; | |
return new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); | |
} | |
} | |
} |
Thanks for the code. I wish you would have referenced aspnet/Mvc#3091 and https://github.com/aspnet/Entropy/blob/dev/samples/Mvc.RenderViewToString/RazorViewToStringRenderer.cs where I assume the code is from.
I just wana say thnx to anybody who worked on this.
THAAAAAAAAAAAAAAAAAAAAAAAAANKS
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, I render a template to a request by ajax post, I get the following exception System.ArgumentOutOfRangeException in line view.RenderAsync (ViewContext) .GetAwaiter () GetResult ().;