我们需要在 WPF 应用程序中自托管一个 Asp.Net Core 应用程序,API 工作正常,但加载 cshtml 视图存在一些问题。
这是我们的代码: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();
}
}
}
主机启动:
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();
}
}
控制器:
[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!");
}
}
网址http://127.0.0.1:9388/api/test有效!(web API) 但是当我们导航到http://127.0.0.1:9388/myview 时,浏览器显示 http 错误 500。
我错过了什么吗?WPF 也不例外。
相关分类