在WPF自托管asp.net核心应用程序中加载视图

编程入门 行业动态 更新时间:2024-10-28 07:23:02
本文介绍了在WPF自托管asp核心应用程序中加载视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们需要在WPF APP中自托管一个Asp.Net Core应用程序,API可以正常工作,但是在加载cshtml视图时会遇到一些问题.

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(); } } }

主机启动:

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!"); } }

URL 127.0.0.1:9388/api/test 正在运行! (网络API) 但是,当我们导航到 127.0.0.1:9388/myview 时,浏览器显示http错误500.

The url 127.0.0.1:9388/api/test is working! (web API) But when we navigate to 127.0.0.1:9388/myview browser shows http error 500.

我错过了什么吗? WPF上没有例外.

Am I missing something? There is no exceptions on WPF.

推荐答案

好的,现在可以使用了! 您需要手动执行一些操作:

Ok, it's working now! You need to do a few things manually:

1)将WPF projet迁移到VS2017格式,方法如下: 如何将Wpf项目迁移到新的VS2017格式(找到@stil答案)

1) Migrate your WPF projet to VS2017 format, here is how: How-to migrate Wpf projects to the new VS2017 format (find for @stil answer)

2)安装Asp Core和Asp Core MVC

2) Install Asp Core And Asp Core MVC

3)再次编辑csproj,并将根Sdk属性从Microsoft.NET.Sdk更改为Microsoft.NET.Sdk.Razor

3) Edit the csproj again and change the root Sdk attribute from Microsoft.NET.Sdk to Microsoft.NET.Sdk.Razor

4)将您的WPF项目指向框架4.6.2,4.7>不起作用!

4) Point your WPF project to framework 4.6.2, 4.7 > doesn't work!

5)您的cshtml必须具有构建选项内容",但是csproj上的指令需要删除,否则它将无法编译.

5) Your cshtml must have the build option "Content", but, the instruction on csproj need to be removed, otherwise, it'll not compile.

这里是一个示例: github/alexandrereyes/wpf-aspnetcore-mvc

更多推荐

在WPF自托管asp.net核心应用程序中加载视图

本文发布于:2023-11-16 18:05:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606592.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   应用程序   加载   核心   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!