Skip to content

Commit 2959cd0

Browse files
committed
Committing the sample
1 parent 45d51eb commit 2959cd0

File tree

9 files changed

+226
-1
lines changed

9 files changed

+226
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
# api-docs-swagger-sample
1+
A sample .net core api project demonstrating the use of swagger to generate automated api docs.
2+
3+
# Reference
4+
https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?view=aspnetcore-2.2

api-docs.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.168
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "api-docs", "src\api-docs.csproj", "{5BDD019A-2CC2-43F5-B7B0-E73DED7DF259}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{5BDD019A-2CC2-43F5-B7B0-E73DED7DF259}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{5BDD019A-2CC2-43F5-B7B0-E73DED7DF259}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{5BDD019A-2CC2-43F5-B7B0-E73DED7DF259}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{5BDD019A-2CC2-43F5-B7B0-E73DED7DF259}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {3975CBBA-6CBD-42D2-8CF3-81CE43677796}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
namespace api_docs.Controllers
8+
{
9+
[Route("api/[controller]")]
10+
[ApiController]
11+
public class PasswordController : ControllerBase
12+
{
13+
// GET api/values
14+
[HttpGet]
15+
public ActionResult<IEnumerable<string>> Get()
16+
{
17+
return new string[] { "value1", "value2" };
18+
}
19+
20+
// GET api/values/5
21+
[HttpGet("{id}")]
22+
public ActionResult<string> Get(int id)
23+
{
24+
return "value";
25+
}
26+
27+
// POST api/values
28+
[HttpPost]
29+
public void Post([FromBody] string value)
30+
{
31+
}
32+
33+
// PUT api/values/5
34+
[HttpPut("{id}")]
35+
public void Put(int id, [FromBody] string value)
36+
{
37+
}
38+
39+
// DELETE api/values/5
40+
[HttpDelete("{id}")]
41+
public void Delete(int id)
42+
{
43+
}
44+
}
45+
}

src/Program.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore;
7+
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.Logging;
10+
11+
namespace api_docs
12+
{
13+
public class Program
14+
{
15+
public static void Main(string[] args)
16+
{
17+
CreateWebHostBuilder(args).Build().Run();
18+
}
19+
20+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
21+
WebHost.CreateDefaultBuilder(args)
22+
.UseStartup<Startup>();
23+
}
24+
}

src/Properties/launchSettings.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:64483",
8+
"sslPort": 44314
9+
}
10+
},
11+
"profiles": {
12+
"IIS Express": {
13+
"commandName": "IISExpress",
14+
"launchBrowser": true,
15+
"launchUrl": "",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"api_docs": {
21+
"commandName": "Project",
22+
"launchBrowser": true,
23+
"launchUrl": "",
24+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
25+
"environmentVariables": {
26+
"ASPNETCORE_ENVIRONMENT": "Development"
27+
}
28+
}
29+
}
30+
}

src/Startup.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.HttpsPolicy;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Configuration;
10+
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.Logging;
12+
using Microsoft.Extensions.Options;
13+
using Swashbuckle.AspNetCore.Swagger;
14+
15+
namespace api_docs
16+
{
17+
public class Startup
18+
{
19+
public Startup(IConfiguration configuration)
20+
{
21+
Configuration = configuration;
22+
}
23+
24+
public IConfiguration Configuration { get; }
25+
26+
// This method gets called by the runtime. Use this method to add services to the container.
27+
public void ConfigureServices(IServiceCollection services)
28+
{
29+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
30+
31+
services.AddSwaggerGen(c =>
32+
{
33+
c.SwaggerDoc("v1", new Info { Title = "API Docs", Version = "v1" });
34+
});
35+
}
36+
37+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
38+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
39+
{
40+
if (env.IsDevelopment())
41+
{
42+
app.UseDeveloperExceptionPage();
43+
}
44+
else
45+
{
46+
app.UseHsts();
47+
}
48+
49+
// Enable middleware to serve generated Swagger as a JSON endpoint.
50+
app.UseSwagger();
51+
52+
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
53+
// specifying the Swagger JSON endpoint.
54+
app.UseSwaggerUI(c =>
55+
{
56+
c.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1");
57+
c.RoutePrefix = string.Empty;
58+
});
59+
60+
app.UseHttpsRedirection();
61+
app.UseMvc();
62+
}
63+
}
64+
}

src/api-docs.csproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Folder Include="wwwroot\" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.AspNetCore.App" />
13+
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
14+
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
15+
</ItemGroup>
16+
17+
</Project>

src/appsettings.Development.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Debug",
5+
"System": "Information",
6+
"Microsoft": "Information"
7+
}
8+
}
9+
}

src/appsettings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Warning"
5+
}
6+
},
7+
"AllowedHosts": "*"
8+
}

0 commit comments

Comments
 (0)