Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default static web page for Katana/Owin self hosted app?

I've set up a web site using an Owin self hosted console app. I'm serving static files with no problem, the 'root' of the static part of the site works properly, and the web API routes work fine also.

If I browse to:

http://localhost/index.html

it presents everything like I expect. But I have not figured out how to set it so that browsing to:

http://localhost

presents index.html (as the default view). This Just Works under an IIS-style site. How do I make it work with Owin self host?

like image 455
wilee Avatar asked Aug 25 '14 02:08

wilee


2 Answers

a more detailed version of fra's answer:

1- NuGet to install Microsoft.Owin.StaticFiles (I assumed you already installed Microsoft.AspNet.WebApi.OwinSelfHost via NuGet)

2- Create a single directory in your solution (in Visual Studio), and put all your client files in it, e.g.

+Web

--+images

--+pages

------page1

------page2

--+scripts

--+css

---index.html

Note: there is a root directory (web) that contains all other directories, and the index.html under the root directly.

3- Now, in the same class that contains your web api routing configuration, add the following code:

var physicalFileSystem = new PhysicalFileSystem(@".\Web"); //. = root, Web = your physical directory that contains all other static content, see prev step
var options = new FileServerOptions
{
    EnableDefaultFiles = true,
    FileSystem = physicalFileSystem
 };
 options.StaticFileOptions.FileSystem = physicalFileSystem;
 options.StaticFileOptions.ServeUnknownFileTypes = true;
 options.DefaultFilesOptions.DefaultFileNames = new[] { "index.html" }; //put whatever default pages you like here
 appBuilder.UseFileServer(options);

4- One more step for the prev code to work: make sure to set the Copy to output directory property of all files in Web directory (and all nested directories) is set to Copy Always [select the file | press F4, or right-click then properties | go to Copy to output directory]

That's all :)

like image 93
Sameh Deabes Avatar answered Sep 21 '22 19:09

Sameh Deabes


I do it this way:

var physicalFileSystem = new PhysicalFileSystem(webPath);
var options = new FileServerOptions
                          {
                              EnableDefaultFiles = true,
                              FileSystem = physicalFileSystem
                          };
        options.StaticFileOptions.FileSystem = physicalFileSystem;
        options.StaticFileOptions.ServeUnknownFileTypes = true;
        options.DefaultFilesOptions.DefaultFileNames = new[] { "index.html" };
        appBuilder.UseFileServer(options);
like image 37
fra Avatar answered Sep 18 '22 19:09

fra