1
0
mirror of https://github.com/tomasvarg/OwinWebApiTest.git synced 2026-03-01 08:28:49 +00:00

Added support for Web & Doc static file servers with rewrite rules prepared for SPA routing

This commit is contained in:
Tomas Varga 2018-01-09 17:26:15 +01:00
parent aee4bd408e
commit 1fdd29c01a
8 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<h1>Documentation</h1>
</body>
</html>

View File

@ -76,6 +76,10 @@
<HintPath>..\packages\Microsoft.Owin.Cors.3.1.0\lib\net45\Microsoft.Owin.Cors.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Owin.FileSystems, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Owin.FileSystems.3.1.0\lib\net45\Microsoft.Owin.FileSystems.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Owin.Host.SystemWeb, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Owin.Host.SystemWeb.3.1.0\lib\net45\Microsoft.Owin.Host.SystemWeb.dll</HintPath>
<Private>True</Private>
@ -92,6 +96,10 @@
<HintPath>..\packages\Microsoft.Owin.Security.OAuth.3.1.0\lib\net45\Microsoft.Owin.Security.OAuth.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Owin.StaticFiles, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Owin.StaticFiles.3.1.0\lib\net45\Microsoft.Owin.StaticFiles.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
@ -138,9 +146,13 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Content Include="Doc\index.html" />
<Content Include="Web.config">
<SubType>Designer</SubType>
</Content>
<Content Include="Web\app.js" />
<Content Include="Web\assets\whatever.html" />
<Content Include="Web\index.html" />
</ItemGroup>
<ItemGroup>
<Compile Include="App_Start\WebApiConfig.cs" />

View File

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

View File

@ -28,7 +28,25 @@
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<remove name="StaticFile"/>
<add name="Owin" verb="" path="*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb"/>
</handlers>
<rewrite>
<rules>
<rule name="Web client config - Single Page App and Docs" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/.*[.].*" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(assets)" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(doc)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

View File

@ -0,0 +1 @@
console.log("app started");

View File

@ -0,0 +1 @@
whatever content

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<h1>Application</h1>
<script src="app.js" type="text/javascript"></script>
</body>
</html>

View File

@ -14,10 +14,12 @@
<package id="Microsoft.Net.Compilers" version="2.6.1" targetFramework="net452" developmentDependency="true" />
<package id="Microsoft.Owin" version="3.1.0" targetFramework="net452" />
<package id="Microsoft.Owin.Cors" version="3.1.0" targetFramework="net452" />
<package id="Microsoft.Owin.FileSystems" version="3.1.0" targetFramework="net452" />
<package id="Microsoft.Owin.Host.SystemWeb" version="3.1.0" targetFramework="net452" />
<package id="Microsoft.Owin.Security" version="3.1.0" targetFramework="net452" />
<package id="Microsoft.Owin.Security.Cookies" version="3.1.0" targetFramework="net452" />
<package id="Microsoft.Owin.Security.OAuth" version="3.1.0" targetFramework="net452" />
<package id="Microsoft.Owin.StaticFiles" version="3.1.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net452" />
<package id="Owin" version="1.0" targetFramework="net452" />
</packages>