From 1fdd29c01a4c5e2d6dc4a6d2565a6c9b39f1eca3 Mon Sep 17 00:00:00 2001 From: Tomas Varga Date: Tue, 9 Jan 2018 17:26:15 +0100 Subject: [PATCH] Added support for Web & Doc static file servers with rewrite rules prepared for SPA routing --- OwinWebApiTest/Doc/index.html | 10 +++++++++ OwinWebApiTest/OwinWebApiTest.csproj | 12 +++++++++++ OwinWebApiTest/Startup.cs | 28 +++++++++++++++++++++++++ OwinWebApiTest/Web.config | 18 ++++++++++++++++ OwinWebApiTest/Web/app.js | 1 + OwinWebApiTest/Web/assets/whatever.html | 1 + OwinWebApiTest/Web/index.html | 11 ++++++++++ OwinWebApiTest/packages.config | 2 ++ 8 files changed, 83 insertions(+) create mode 100644 OwinWebApiTest/Doc/index.html create mode 100644 OwinWebApiTest/Web/app.js create mode 100644 OwinWebApiTest/Web/assets/whatever.html create mode 100644 OwinWebApiTest/Web/index.html diff --git a/OwinWebApiTest/Doc/index.html b/OwinWebApiTest/Doc/index.html new file mode 100644 index 0000000..a782963 --- /dev/null +++ b/OwinWebApiTest/Doc/index.html @@ -0,0 +1,10 @@ + + + + + + + +

Documentation

+ + diff --git a/OwinWebApiTest/OwinWebApiTest.csproj b/OwinWebApiTest/OwinWebApiTest.csproj index a2b6256..9c9dca4 100644 --- a/OwinWebApiTest/OwinWebApiTest.csproj +++ b/OwinWebApiTest/OwinWebApiTest.csproj @@ -76,6 +76,10 @@ ..\packages\Microsoft.Owin.Cors.3.1.0\lib\net45\Microsoft.Owin.Cors.dll True + + ..\packages\Microsoft.Owin.FileSystems.3.1.0\lib\net45\Microsoft.Owin.FileSystems.dll + True + ..\packages\Microsoft.Owin.Host.SystemWeb.3.1.0\lib\net45\Microsoft.Owin.Host.SystemWeb.dll True @@ -92,6 +96,10 @@ ..\packages\Microsoft.Owin.Security.OAuth.3.1.0\lib\net45\Microsoft.Owin.Security.OAuth.dll True + + ..\packages\Microsoft.Owin.StaticFiles.3.1.0\lib\net45\Microsoft.Owin.StaticFiles.dll + True + ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll True @@ -138,9 +146,13 @@ + Designer + + + diff --git a/OwinWebApiTest/Startup.cs b/OwinWebApiTest/Startup.cs index c999a2e..3b83649 100644 --- a/OwinWebApiTest/Startup.cs +++ b/OwinWebApiTest/Startup.cs @@ -1,10 +1,13 @@ using System; using System.Configuration; +using System.IO; using System.Threading.Tasks; using Owin; using Microsoft.Owin; using Microsoft.Owin.Cors; using Microsoft.Owin.Security.OAuth; +using Microsoft.Owin.FileSystems; +using Microsoft.Owin.StaticFiles; using System.Web.Http; using System.Net; @@ -42,6 +45,31 @@ namespace OwinWebApiTest // allow self-signed certificates ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; + + string webDir = ConfigurationManager.AppSettings["WebDirectory"]; + if (string.IsNullOrEmpty(webDir)) webDir = "Web"; + app.UseFileServer(GetFileServerOptions(PathString.Empty, webDir)); + + string docDir = ConfigurationManager.AppSettings["DocDirectory"]; + if (string.IsNullOrEmpty(docDir)) docDir = "Doc"; + app.UseFileServer(GetFileServerOptions(new PathString("/doc"), docDir)); + } + + private FileServerOptions GetFileServerOptions(PathString pathString, string dir) + { + string appRoot = AppDomain.CurrentDomain.BaseDirectory; + var fileSystem = new PhysicalFileSystem(Path.Combine(appRoot, dir)); + + var options = new FileServerOptions + { + RequestPath = pathString, + EnableDefaultFiles = true, + FileSystem = fileSystem + }; + options.StaticFileOptions.FileSystem = fileSystem; + options.StaticFileOptions.ServeUnknownFileTypes = false; + + return options; } } } diff --git a/OwinWebApiTest/Web.config b/OwinWebApiTest/Web.config index d4e2984..b11c3ce 100644 --- a/OwinWebApiTest/Web.config +++ b/OwinWebApiTest/Web.config @@ -28,7 +28,25 @@ + + + + + + + + + + + + + + + + + + diff --git a/OwinWebApiTest/Web/app.js b/OwinWebApiTest/Web/app.js new file mode 100644 index 0000000..669509d --- /dev/null +++ b/OwinWebApiTest/Web/app.js @@ -0,0 +1 @@ +console.log("app started"); \ No newline at end of file diff --git a/OwinWebApiTest/Web/assets/whatever.html b/OwinWebApiTest/Web/assets/whatever.html new file mode 100644 index 0000000..eb2f1e0 --- /dev/null +++ b/OwinWebApiTest/Web/assets/whatever.html @@ -0,0 +1 @@ +whatever content \ No newline at end of file diff --git a/OwinWebApiTest/Web/index.html b/OwinWebApiTest/Web/index.html new file mode 100644 index 0000000..3cb2df9 --- /dev/null +++ b/OwinWebApiTest/Web/index.html @@ -0,0 +1,11 @@ + + + + + + + +

Application

+ + + diff --git a/OwinWebApiTest/packages.config b/OwinWebApiTest/packages.config index e21f221..5f48562 100644 --- a/OwinWebApiTest/packages.config +++ b/OwinWebApiTest/packages.config @@ -14,10 +14,12 @@ + + \ No newline at end of file