Skip to content

Instantly share code, notes, and snippets.

@wahmedswl
Created December 14, 2020 16:59
Show Gist options
  • Save wahmedswl/1d04bd7bc01817532e2bf137f3ab274e to your computer and use it in GitHub Desktop.
Save wahmedswl/1d04bd7bc01817532e2bf137f3ab274e to your computer and use it in GitHub Desktop.
OWIN Hosting with multiple Urls and related configuration
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