【问题标题】:Self host asp core - angular 2 site with OWIN as console app自托管 asp 核心 - 以 OWIN 作为控制台应用程序的 Angular 2 站点
【发布时间】:2017-11-22 23:53:53
【问题描述】:

我已经用 ASP net core 2 和 angular 构建了一个 Angular 项目,我一直试图在 OWIN 中托管它但无济于事。我使用 VS publish 来生成要发布的文件,当直接从 Visual Studio 在 IIS express 中运行时效果很好。

这是发布文件夹的文件结构:

MainDirectory
    wwwroot
       dist
       favicon

我已经在 .NET (Visual Studio) 中设置了一个发布项目,并且在我的 startup.cs 类中有以下内容,但是当我在启动服务后浏览到 url 时,页面中没有任何内容:我在下面使用了这段代码.

const string rootFolder = "C:\\src\\StartupService\\maindirectory";
            var fileSystem = new PhysicalFileSystem(rootFolder);
            var options = new FileServerOptions
            {
                EnableDefaultFiles = true,
                FileSystem = fileSystem
            };

            app.UseFileServer(options);

我想提一下,我在 C# 中使用 app.UseWelcome() 页面测试了 OWIN 自托管服务器,它很好,但它不会为我的 Angular 站点提供服务。顺便提一下,我在发布文件夹中的任何地方都看不到任何 html 文件,只有 DLL、JSON 文件和 web.config,所以我不知道这是否有问题。

我也试过这段代码,但也没有用。

var physicalFileSystem = new PhysicalFileSystem("C:\\src\\StartupService\\maindirectory");
            var options = new FileServerOptions
            {
                EnableDefaultFiles = true,
                FileSystem = physicalFileSystem,
                StaticFileOptions =
                {
                    FileSystem = physicalFileSystem,
                    ServeUnknownFileTypes = true
                },
                // DefaultFilesOptions = {DefaultFileNames = new[] {"index.html"}}
            };
            app.UseFileServer(options);

知道如何在 OWIN 中自行托管使用 VS2017 创建的 Angular 2 吗?

【问题讨论】:

    标签: .net angular asp.net-core owin asp.net-core-mvc-2.0


    【解决方案1】:

    尝试将相对路径(例如 @".\Public" )传递给 PhysicalFileSystem 对象。

    这是一个扩展助手;

     public static class FileServerConfig
        {
            internal static void ServeFiles(this IAppBuilder app, string path = @".\Public")
            {
                try
                {
                    var fileServerOptions = new FileServerOptions()
                    {
                        EnableDefaultFiles = true,
                        EnableDirectoryBrowsing = false,
                        RequestPath = new PathString(string.Empty),
                        FileSystem = new PhysicalFileSystem(path)
                    };
    
                    fileServerOptions.StaticFileOptions.ServeUnknownFileTypes = true;
    
                    app.UseFileServer(fileServerOptions);
                }
                catch (System.Exception)
                {
                    // Serilog.Log.Fatal("Cannot locate Public folder"); // Logging
                }
            }
        }
    

    将上述代码添加到您应用中的新类中,然后在 Startup.cs 中执行以下任一操作:

    app.ServeFiles();
    

    app.ServeFiles(@".\customPath");
    

    还有;确保将 Owin 处理程序添加到 web.config

    <handlers> ... <add name="Owin" verb="" path="*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb" /> </handlers>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-21
      • 1970-01-01
      • 2017-08-30
      • 2018-04-11
      • 1970-01-01
      • 1970-01-01
      • 2018-05-14
      相关资源
      最近更新 更多