Created
December 14, 2020 16:59
-
-
Save wahmedswl/1d04bd7bc01817532e2bf137f3ab274e to your computer and use it in GitHub Desktop.
OWIN Hosting with multiple Urls and related configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static IDisposable HttpInterface(Setting setting, Interface @interface = null, string url = null, bool adminApi = false) | |
{ | |
var httpInterface = WebApp.Start(url ?? @interface.Http.Url, (app) => | |
{ | |
var config = new HttpConfiguration(); | |
config.Formatters.JsonFormatter.MediaTypeMappings.Add(new System.Net.Http.Formatting.RequestHeaderMapping("Accept", | |
"text/html", | |
StringComparison.InvariantCultureIgnoreCase, | |
true, | |
"application/json")); | |
config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; | |
config.MapHttpAttributeRoutes(); | |
config.Routes.MapHttpRoute( | |
"DefaultApi", | |
"api/{controller}/{id}", | |
new { id = RouteParameter.Optional }); | |
var builder = new ContainerBuilder(); | |
builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); | |
builder.RegisterWebApiFilterProvider(config); | |
builder.RegisterInstance(setting.Global); | |
if (@interface != null) | |
{ | |
builder.RegisterInstance(@interface); | |
} | |
var container = builder.Build(); | |
config.DependencyResolver = new AutofacWebApiDependencyResolver(container); | |
app.UseAutofacMiddleware(container); | |
app.UseAutofacWebApi(config); | |
app.UseWebApi(config); | |
if (adminApi) | |
{ | |
var mountPath = new PhysicalFileSystem(@"./www"); | |
var options = new FileServerOptions | |
{ | |
EnableDefaultFiles = true, | |
FileSystem = mountPath | |
}; | |
options.StaticFileOptions.FileSystem = mountPath; | |
options.StaticFileOptions.ServeUnknownFileTypes = true; | |
options.DefaultFilesOptions.DefaultFileNames = new[] | |
{ | |
"index.html" | |
}; | |
app.UseFileServer(options); | |
} | |
}); | |
return httpInterface; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment