We need to self-host an Asp.Net Core application in a WPF APP, APIs are working fine but there is some issues loading cshtml views.
Here is our code:
Host Builder:
public static class HostBuilder
{
private static IWebHost host;
public static async Task Start()
{
if (host == null)
{
var ip = System.Net.IPAddress.Parse("127.0.0.1");
var port = 9388;
host = new WebHostBuilder()
.UseKestrel(options =>
{
options.Listen(ip, port);
})
.UseStartup<HostStartup>()
.Build();
await host.RunAsync();
}
}
}
Host startup:
public class HostStartup
{
public IConfiguration Configuration { get; }
public HostStartup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, System.IServiceProvider serviceProvider)
{
app.UseMvcWithDefaultRoute();
}
}
Controller:
[AllowAnonymous]
[Route("api")]
public class LoginController : Controller
{
[HttpGet]
[Route("/myview")]
public ViewResult MyView()
{
var v = View("myview");
return v;
}
[HttpGet]
[Route("test")]
public IActionResult Test()
{
return Ok("Good!");
}
}
The url http://127.0.0.1:9388/api/test is working! (web API)
But when we navigate to http://127.0.0.1:9388/myview browser shows http error 500.
Am I missing something? There is no exceptions on WPF.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…