• <bdo id='43K0X'></bdo><ul id='43K0X'></ul>
      <tfoot id='43K0X'></tfoot>

        <small id='43K0X'></small><noframes id='43K0X'>

        <legend id='43K0X'><style id='43K0X'><dir id='43K0X'><q id='43K0X'></q></dir></style></legend>
        <i id='43K0X'><tr id='43K0X'><dt id='43K0X'><q id='43K0X'><span id='43K0X'><b id='43K0X'><form id='43K0X'><ins id='43K0X'></ins><ul id='43K0X'></ul><sub id='43K0X'></sub></form><legend id='43K0X'></legend><bdo id='43K0X'><pre id='43K0X'><center id='43K0X'></center></pre></bdo></b><th id='43K0X'></th></span></q></dt></tr></i><div id='43K0X'><tfoot id='43K0X'></tfoot><dl id='43K0X'><fieldset id='43K0X'></fieldset></dl></div>

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

        Loading views in WPF self-host asp.net core application(在 WPF 自托管 asp.net 核心应用程序中加载视图)
        • <tfoot id='D89te'></tfoot>

          <i id='D89te'><tr id='D89te'><dt id='D89te'><q id='D89te'><span id='D89te'><b id='D89te'><form id='D89te'><ins id='D89te'></ins><ul id='D89te'></ul><sub id='D89te'></sub></form><legend id='D89te'></legend><bdo id='D89te'><pre id='D89te'><center id='D89te'></center></pre></bdo></b><th id='D89te'></th></span></q></dt></tr></i><div id='D89te'><tfoot id='D89te'></tfoot><dl id='D89te'><fieldset id='D89te'></fieldset></dl></div>

          <small id='D89te'></small><noframes id='D89te'>

            <tbody id='D89te'></tbody>

            • <bdo id='D89te'></bdo><ul id='D89te'></ul>

              <legend id='D89te'><style id='D89te'><dir id='D89te'><q id='D89te'></q></dir></style></legend>
                1. 本文介绍了在 WPF 自托管 asp.net 核心应用程序中加载视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我们需要在 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!");
                          }
                      }
                  

                  网址 http://127.0.0.1:9388/api/test 有效!(网络 API)但是当我们导航到 http://127.0.0.1:9388/myview 浏览器显示 http 错误 500.

                  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.

                  我错过了什么吗?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 项目迁移到 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.net Core 和 Asp.net Core MVC

                  2) Install Asp.net Core And Asp.net 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必须有构建选项Content",但是,csproj上的指令需要去掉,否则编译不出来.

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

                  这是一个例子:https://github.com/alexandrereyes/wpf-aspnetcore-mvc

                  这篇关于在 WPF 自托管 asp.net 核心应用程序中加载视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  C# Attributes and their uses(C# 属性及其用途)
                  C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)

                  1. <tfoot id='iZJvw'></tfoot>
                  2. <legend id='iZJvw'><style id='iZJvw'><dir id='iZJvw'><q id='iZJvw'></q></dir></style></legend>

                    <small id='iZJvw'></small><noframes id='iZJvw'>

                        • <bdo id='iZJvw'></bdo><ul id='iZJvw'></ul>
                              <tbody id='iZJvw'></tbody>

                            <i id='iZJvw'><tr id='iZJvw'><dt id='iZJvw'><q id='iZJvw'><span id='iZJvw'><b id='iZJvw'><form id='iZJvw'><ins id='iZJvw'></ins><ul id='iZJvw'></ul><sub id='iZJvw'></sub></form><legend id='iZJvw'></legend><bdo id='iZJvw'><pre id='iZJvw'><center id='iZJvw'></center></pre></bdo></b><th id='iZJvw'></th></span></q></dt></tr></i><div id='iZJvw'><tfoot id='iZJvw'></tfoot><dl id='iZJvw'><fieldset id='iZJvw'></fieldset></dl></div>