Last active
July 11, 2020 14:42
-
-
Save georgekosmidis/f5b4a75fc556a84fa4f7d224094b0a1d to your computer and use it in GitHub Desktop.
These gists are part of the articles: https://blog.georgekosmidis.net/2020/07/11/get-started-with-swagger-and-asp-net-core/ and https://blog.georgekosmidis.net/2020/07/11/swagger-in-asp-net-core-tips-and-tricks/
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 void Configure(IApplicationBuilder app) | |
{ | |
//... | |
// Enable middleware to serve generated Swagger as a JSON endpoint. | |
app.UseSwagger(); | |
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), | |
// specifying the Swagger JSON endpoint. | |
app.UseSwaggerUI(c => | |
{ | |
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); | |
}); | |
//... | |
} |
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 void ConfigureServices(IServiceCollection services) | |
{ | |
//... | |
// Register the Swagger generator, defining 1 or more Swagger documents | |
services.AddSwaggerGen(); | |
//... | |
} |
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
/// <summary> | |
/// An object that represents an address | |
/// </summary> | |
public class AddressModel | |
{ | |
/// <summary> | |
/// The Street part of the address | |
/// </summary> | |
[MinLength(1), MaxLength(255)] | |
public string Street { get; set; } | |
/// <summary> | |
/// The number of the street | |
/// </summary> | |
[MinLength(1), MaxLength(35)] | |
public string StreetNumber { get; set; } | |
/// <summary> | |
/// The City | |
/// </summary> | |
[MinLength(3), MaxLength(40)] | |
public string City { get; set; } | |
/// <summary> | |
/// The ZIP Code | |
/// </summary> | |
[MinLength(4), MaxLength(20)] | |
public string Zip { get; set; } | |
/// <summary> | |
/// The Country | |
/// | |
/// NOTE: This should be the ISO-3166-1 Alpha-2 code for countries (https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#NL), i.e. NL for Netherlands. | |
/// </summary> | |
public string Country { get; set; } | |
} |
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 void Configure(IApplicationBuilder app) | |
{ | |
app.UseStaticFiles(); | |
//... | |
} |
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
services.AddSwaggerGen(c => | |
{ | |
// Set the comments path for the Swagger JSON and UI. | |
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; | |
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); | |
c.IncludeXmlComments(xmlPath); | |
}); |
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
app.UseSwaggerUI(c => | |
{ | |
c.InjectStylesheet("/your_path/your_theme.css"); | |
} |
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
services.AddSwaggerGen(c => | |
{ | |
c.SwaggerDoc("v1", new OpenApiInfo | |
{ | |
Version = "v1", | |
Title = "The .NET Lab API", | |
Description = "An imaginary API of my blog", | |
TermsOfService = new Uri("https://blog.georgekosmidis.net/privacy-policy/"), | |
Contact = new OpenApiContact | |
{ | |
Name = "George Kosmidis", | |
Email = string.Empty, | |
Url = new Uri("https://georgekosmidis.net"), | |
}, | |
License = new OpenApiLicense | |
{ | |
Name = "Use under MIT", | |
Url = new Uri("https://blog.georgekosmidis.net/privacy-policy/"), | |
} | |
}); | |
}); |
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
/// <summary> | |
/// Creates a TodoItem. | |
/// </summary> | |
/// <remarks> | |
/// Sample request: | |
/// | |
/// POST /Todo | |
/// { | |
/// "id": 1, | |
/// "name": "Item1", | |
/// "isComplete": true | |
/// } | |
/// | |
/// </remarks> | |
/// <param name="item"></param> | |
/// <returns>A newly created TodoItem</returns> | |
/// <response code="201">Returns the newly created item</response> //Describe the response 201 | |
/// <response code="400">If the item is null</response> //Describe the response 400 | |
[HttpPost] | |
[ProducesResponseType(StatusCodes.Status201Created)] //Instruct Swagger about the 201 | |
[ProducesResponseType(StatusCodes.Status400BadRequest)] //Instruct Swagger about the 400 | |
public ActionResult<TodoItem> Create(TodoItem item) | |
{ | |
_context.TodoItems.Add(item); | |
_context.SaveChanges(); | |
return CreatedAtRoute("GetTodo", new { id = item.Id }, item); | |
} |
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
app.UseSwaggerUI(c => | |
{ | |
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); | |
c.RoutePrefix = string.Empty; | |
}); |
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
app.UseSwagger(c => | |
{ | |
c.SerializeAsV2 = true; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment