Created
November 11, 2015 16:41
-
-
Save stevenkuhn/6d61735d83b4dd0a1fe6 to your computer and use it in GitHub Desktop.
Swagger File Generation With Swashbuckle
This file contains hidden or 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 class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
if (args == null || args.Length == 0) throw new InvalidOperationException("You must specify arguments."); | |
var path = args[0]; | |
using (var server = TestServer.Create<Startup>()) | |
{ | |
var response = server.CreateRequest("/swagger/docs/v1").GetAsync().Result; | |
var content = response.Content.ReadAsStringAsync().Result; | |
File.WriteAllText(path, content); | |
} | |
} | |
} |
This file contains hidden or 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 class Startup | |
{ | |
public void Configuration(IAppBuilder app) | |
{ | |
var config = new HttpConfiguration(); | |
var container = ConfigureAutofac(app, config); | |
WebApiConfig.Register(config); | |
app.UseWebApi(config); | |
} | |
public virtual IContainer ConfigureAutofac(IAppBuilder app, HttpConfiguration config) | |
{ | |
var builder = new ContainerBuilder(); | |
builder.RegisterAssemblyModules(typeof(ApiStartup).Assembly); | |
var container = builder.Build(); | |
// Allows the Autofac container to be accessed from the OWIN context. | |
app.UseAutofacMiddleware(container); | |
// Informs ASP.NET WebApi 2 to use Autofac when resolving dependencies | |
// (e.g. dependencies in Controllers). | |
config.DependencyResolver = new AutofacWebApiDependencyResolver(container); | |
return container; | |
} | |
} |
This file contains hidden or 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 class WebApiConfig | |
{ | |
public static void Register(HttpConfiguration config) | |
{ | |
// Web API configuration and services | |
var formatters = config.Formatters; | |
formatters.Remove(formatters.XmlFormatter); | |
var jqueryFormatter = formatters.FirstOrDefault(x => x.GetType() == typeof(JQueryMvcFormUrlEncodedFormatter)); | |
formatters.Remove(formatters.XmlFormatter); | |
formatters.Remove(formatters.FormUrlEncodedFormatter); | |
formatters.Remove(jqueryFormatter); | |
JsonMediaTypeFormatter json = config.Formatters.JsonFormatter; | |
JsonSerializerSettings settings = json.SerializerSettings; | |
settings.Formatting = Newtonsoft.Json.Formatting.Indented; | |
settings.ContractResolver = new CamelCasePropertyNamesContractResolver(); | |
settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Include; | |
settings.DateParseHandling = Newtonsoft.Json.DateParseHandling.DateTimeOffset; | |
settings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat; | |
settings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc; | |
settings.Converters.Add(new StringEnumConverter()); | |
// Web API routes | |
config.MapHttpAttributeRoutes(); | |
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; | |
SwaggerConfig.Register(config); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment