Skip to content

Commit 3e75775

Browse files
Added Swashbuckle.AspNetCore Package, Startup Setup and added XML documentation to TestRegistersController
1 parent 3c7c05f commit 3e75775

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed

src/jQueryDatatableServerSideNetCore/Controllers/TestRegistersController.cs

+28-2
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,44 @@
1313

1414
namespace jQueryDatatableServerSideNetCore.Controllers
1515
{
16+
/// <summary>
17+
/// <br />
18+
/// </summary>
19+
[ApiController]
20+
[Route("[controller]")]
1621
public class TestRegistersController : Controller
1722
{
1823
private readonly ApplicationDbContext _context;
1924
private readonly IExportService _exportService;
2025

26+
/// <summary>Initializes a new instance of the <see cref="TestRegistersController" /> class.</summary>
27+
/// <param name="context">The context.</param>
28+
/// <param name="exportService">The export service.</param>
2129
public TestRegistersController(ApplicationDbContext context, IExportService exportService)
2230
{
2331
_context = context;
2432
_exportService = exportService;
2533
}
2634

2735
// GET: TestRegisters
36+
/// <summary>Indexes this instance.</summary>
37+
/// <returns>
38+
/// <br />
39+
/// </returns>
40+
[HttpGet]
2841
public async Task<IActionResult> Index()
2942
{
3043
await SeedData();
3144

3245
return View();
3346
}
3447

35-
[HttpPost]
48+
/// <summary>Loads the table.</summary>
49+
/// <param name="dtParameters">The dt parameters.</param>
50+
/// <returns>
51+
/// <br />
52+
/// </returns>
53+
[HttpPost("LoadTable")]
3654
public async Task<IActionResult> LoadTable([FromBody] DtParameters dtParameters)
3755
{
3856
var searchBy = dtParameters.Search?.Value;
@@ -80,7 +98,13 @@ public async Task<IActionResult> LoadTable([FromBody] DtParameters dtParameters)
8098
});
8199
}
82100

83-
[HttpPost]
101+
/// <summary>Exports the table.</summary>
102+
/// <param name="format">The format.</param>
103+
/// <param name="dtParametersJson">The dt parameters json.</param>
104+
/// <returns>
105+
/// <br />
106+
/// </returns>
107+
[HttpPost("ExportTable")]
84108
public async Task<IActionResult> ExportTable([FromQuery] string format, [FromForm] string dtParametersJson)
85109
{
86110
var dtParameters = new DtParameters();
@@ -136,6 +160,8 @@ public async Task<IActionResult> ExportTable([FromQuery] string format, [FromFor
136160
return null;
137161
}
138162

163+
/// <summary>Seeds the data.</summary>
164+
[NonAction]
139165
public async Task SeedData()
140166
{
141167
if (!_context.TestRegisters.Any())

src/jQueryDatatableServerSideNetCore/Properties/launchSettings.json

+2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
"IIS Express": {
1212
"commandName": "IISExpress",
1313
"launchBrowser": true,
14+
"launchUrl": "TestRegisters",
1415
"environmentVariables": {
1516
"ASPNETCORE_ENVIRONMENT": "Development"
1617
}
1718
},
1819
"jQueryDatatableServerSideNetCore": {
1920
"commandName": "Project",
2021
"launchBrowser": true,
22+
"launchUrl": "TestRegisters",
2123
"applicationUrl": "https://localhost:5001;http://localhost:5000",
2224
"environmentVariables": {
2325
"ASPNETCORE_ENVIRONMENT": "Development"

src/jQueryDatatableServerSideNetCore/Startup.cs

+25-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
using Microsoft.Extensions.Configuration;
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.Hosting;
10+
using Microsoft.OpenApi.Models;
1011
using Newtonsoft.Json.Converters;
12+
using System;
13+
using System.IO;
14+
using System.Linq;
15+
using System.Reflection;
1116

1217
namespace jQueryDatatableServerSideNetCore
1318
{
@@ -38,6 +43,17 @@ public void ConfigureServices(IServiceCollection services)
3843
services.AddRazorPages();
3944

4045
services.AddScoped<IExportService, ExportService>();
46+
47+
// Register the Swagger generator, defining 1 or more Swagger documents
48+
services.AddSwaggerGen(c =>
49+
{
50+
c.SwaggerDoc("v1", new OpenApiInfo { Title = "jQueryDatatableServerSideNetCore", Version = "v1" });
51+
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.FirstOrDefault());
52+
53+
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
54+
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
55+
c.IncludeXmlComments(xmlPath);
56+
});
4157
}
4258

4359
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -57,6 +73,14 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
5773
app.UseHttpsRedirection();
5874
app.UseStaticFiles();
5975

76+
// Enable middleware to serve generated Swagger as a JSON endpoint.
77+
app.UseSwagger();
78+
app.UseSwaggerUI(c =>
79+
{
80+
c.RoutePrefix = "docs";
81+
c.SwaggerEndpoint("/swagger/v1/swagger.json", "jQueryDatatableServerSideNetCore Docs v1");
82+
});
83+
6084
app.UseRouting();
6185

6286
app.UseAuthentication();
@@ -66,7 +90,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
6690
{
6791
endpoints.MapControllerRoute(
6892
name: "default",
69-
pattern: "{controller=Home}/{action=Index}/{id?}");
93+
pattern: "{controller=TestRegisters}/{action=Index}/{id?}");
7094
endpoints.MapRazorPages();
7195
});
7296
}

src/jQueryDatatableServerSideNetCore/Views/Shared/_Layout.cshtml

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
<li class="nav-item">
3333
<a class="nav-link text-dark" asp-area="" asp-controller="TestRegisters" asp-action="Index">Test DataTable</a>
3434
</li>
35+
<li class="nav-item">
36+
<a class="nav-link text-dark" href="docs" target="_blank">Swagger DOCS <span class="badge rounded-pill bg-info text-dark">New</span></a>
37+
</li>
3538
</ul>
3639
</div>
3740
</div>

src/jQueryDatatableServerSideNetCore/jQueryDatatableServerSideNetCore.csproj

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
<UserSecretsId>aspnet-jQueryDatatableServerSideNetCore-F30D421D-7C39-47E7-8247-3E3BE3C0B128</UserSecretsId>
66
</PropertyGroup>
77

8+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
9+
<DocumentationFile>obj\Debug\net5.0\jQueryDatatableServerSideNetCore.xml</DocumentationFile>
10+
</PropertyGroup>
11+
12+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
13+
<DocumentationFile>obj\Release\net5.0\jQueryDatatableServerSideNetCore.xml</DocumentationFile>
14+
</PropertyGroup>
815

916
<ItemGroup>
1017
<PackageReference Include="EPPlus" Version="5.8.0" />
@@ -18,6 +25,7 @@
1825
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1926
</PackageReference>
2027
<PackageReference Include="RandomGen" Version="1.1.5" />
28+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
29+
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.2.3" />
2130
</ItemGroup>
22-
2331
</Project>

0 commit comments

Comments
 (0)