james Makinde
james Makinde

Reputation: 973

Self host asp core - Angular 2 site with OWIN as console app

I have built an Angular project with ASP net core 2 and I have been trying to host it in OWIN to no avail. I used VS publish to generate the files to publish which works fine when run in IIS express directly from Visual Studio.

This is the file structure for the publish folders:

MainDirectory
    wwwroot
       dist
       favicon

I have setup a publish project in .NET (Visual Studio) and have the following in my startup.cs class but when I browse to the url after starting the service, there is nothing in the page: I used this code below.

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

            app.UseFileServer(options);

I want to mention that I tested the OWIN self host server with the app.UseWelcome() page in C# and it was fine but it won't serve my Angular site. Just to mentioned that I don't see any html files in the publish folder anywhere, just DLLs, JSON files and web.config, so I don't know if that's a problem.

I also tried this code, but it didn't work too.

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

Any idea how to self host an Angular 2 created with VS2017 in OWIN?

Upvotes: 0

Views: 1356

Answers (1)

Liam Fleming
Liam Fleming

Reputation: 1106

Try passing a relative path, such as @".\Public" to the PhysicalFileSystem object.

Here's an extension helper;

 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
            }
        }
    }

Add the above code to a new class in your app and then in Startup.cs do either:

app.ServeFiles();

or

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

Also; be sure to add the Owin handler to the web.config

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

Upvotes: 1

Related Questions