使用OWIN静态文件定义主页面

时间:2017-11-08 10:17:30

标签: c# static owin self-hosting

我在OWIN的帮助下拥有一个自托管的应用程序。另外,我需要提供一些静态文件。它工作但最近我遇到了一个问题 - 默认文件停止工作。我的意思是,如果你去localhost,它会将你重定向到" main"页。但现在主页只能通过直接路径访问,例如https://localhost:4443/index.html
这是我的启动方法:

public class Startup
{

    // This code configures Web API. The Startup class is specified as a type
    // parameter in the WebApp.Start method.
    public void Configuration(IAppBuilder appBuilder)
    {
        try
        {
            appBuilder.UseOAuthAuthorizationServer(new OAuthOptions());
            appBuilder.UseJwtBearerAuthentication(new JwtOptions());

            var config = new MyHttpsSelfHostConfiguration("https://localhost:4443");

            var start = DateTime.Now;

            config.MapHttpAttributeRoutes();
            var jsonFormatter = new JsonMediaTypeFormatter();
            config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));

            config.Routes.MapHttpRoute(
                name: "WebLabApp",
                routeTemplate: "api/{controller}/{action}/{id}"
            );

            ConfigureUnity(config);
            appBuilder.UseWebApi(config);



            Console.WriteLine($"Configure Unity in {DateTime.Now - start}");
            start = DateTime.Now;

            BusinessServicesHostLibrary.ContainerRegistry.RegisterAllBusinessServicesCommonClassesAndConnectionFactory();
            Console.WriteLine($"Register all in {DateTime.Now - start}");
            #if DEBUG
            var contentDir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent?.FullName;
            #else
            var contentDir = @".";
            #endif
            var fileSystem = new PhysicalFileSystem(contentDir);
            var options = new FileServerOptions
            {
                EnableDefaultFiles = true,
                DefaultFilesOptions = { DefaultFileNames = { "index.html" } },
                FileSystem = fileSystem,
            };
            options.StaticFileOptions.FileSystem = fileSystem;
            options.StaticFileOptions.ServeUnknownFileTypes = true;
            appBuilder.UseFileServer(options);
            config.EnsureInitialized();
        }
        catch (Exception ex)
        {
            if (ex is System.Reflection.ReflectionTypeLoadException)
            {
                var typeLoadException = ex as ReflectionTypeLoadException;
                var loaderExceptions = typeLoadException.LoaderExceptions;
                foreach (var x in loaderExceptions) Console.WriteLine(x);
            }
            throw;
        }
    }

我做错了什么?

1 个答案:

答案 0 :(得分:0)

解决方案非常简单 - 我们需要致电appBuilder.UseWebApi(config); 在配置方法

的末尾