Created
June 26, 2019 15:31
-
-
Save teedty1/a505b2a4fb2c48658d127d5d4682628a to your computer and use it in GitHub Desktop.
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.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Threading.Tasks; | |
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; | |
using Microsoft.Extensions.DependencyInjection; | |
using Nancy; | |
using Nancy.Extensions; | |
using Nancy.Responses; | |
using Nancy.ViewEngines; | |
namespace www.SupportClasses | |
{ | |
public class NancyRazorViewEngine : IViewEngine | |
{ | |
private readonly IRazorViewEngine _razorViewEngine; | |
private readonly ITempDataProvider _tempDataProvider; | |
private readonly IServiceProvider _serviceProvider; | |
public NancyRazorViewEngine(IServiceProvider dotNetServices) | |
{ | |
var scope = dotNetServices.CreateScope(); | |
_razorViewEngine = scope.ServiceProvider.GetService<IRazorViewEngine>(); | |
_tempDataProvider = scope.ServiceProvider.GetService<ITempDataProvider>(); | |
_serviceProvider = scope.ServiceProvider; | |
} | |
public void Initialize(ViewEngineStartupContext viewEngineStartupContext) | |
{ | |
} | |
public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext) | |
{ | |
var viewName = viewLocationResult.Location + "/" + viewLocationResult.Name; | |
var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider }; | |
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); | |
using (var sw = new StringWriter()) | |
{ | |
var viewResult = _razorViewEngine.FindView(actionContext, 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() | |
); | |
viewResult.View.RenderAsync(viewContext).Wait(); | |
var response = new HtmlResponse | |
{ | |
Contents = stream => | |
{ | |
using (var writer = new StreamWriter(stream)) | |
{ | |
writer.Write(sw.ToString()); | |
writer.Flush(); | |
} | |
} | |
}; | |
return response; | |
} | |
} | |
public IEnumerable<string> Extensions => new[] { "cshtml" }; | |
} | |
public class NancyViewLocationExpander : IViewLocationExpander | |
{ | |
public void PopulateValues(ViewLocationExpanderContext context) | |
{ | |
} | |
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) | |
{ | |
yield return "/{0}.cshtml"; | |
} | |
} | |
} |
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.Diagnostics; | |
using System.IO; | |
using System.Reflection; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Hosting.Internal; | |
using Microsoft.AspNetCore.Mvc.Razor; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.FileProviders; | |
using Microsoft.Extensions.ObjectPool; | |
using Microsoft.Extensions.PlatformAbstractions; | |
using Nancy.Owin; | |
using www.SupportClasses; | |
namespace www | |
{ | |
public class Startup | |
{ | |
private readonly IConfiguration _config; | |
private IServiceCollection _services; | |
public Startup(IConfiguration configuration) | |
{ | |
_config = configuration; | |
} | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
var applicationEnvironment = PlatformServices.Default.Application; | |
services.AddSingleton(applicationEnvironment); | |
var appDirectory = Directory.GetCurrentDirectory(); | |
var environment = new HostingEnvironment | |
{ | |
ApplicationName = Assembly.GetEntryAssembly().GetName().Name | |
}; | |
services.AddSingleton<IHostingEnvironment>(environment); | |
services.Configure<RazorViewEngineOptions>(options => | |
{ | |
options.FileProviders.Clear(); | |
options.FileProviders.Add(new PhysicalFileProvider(appDirectory)); | |
options.ViewLocationExpanders.Clear(); | |
options.ViewLocationExpanders.Add(new NancyViewLocationExpander()); | |
}); | |
services.AddMvc(); | |
_services = services; | |
} | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | |
{ | |
app | |
.UseOwin(owin => owin | |
.UseNancy(nancy => | |
{ | |
nancy.Bootstrapper = new Bootstrapper(_config, app.ApplicationServices); | |
}) | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does it work on 3.1.1?