diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..dc0d928 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "C#: Debug", + "type": "dotnet", + "request": "launch", + "projectPath": "${workspaceFolder}/DoSomethingService/DoSomethingService.csproj" + } + + ] +} \ No newline at end of file diff --git a/DoSomethingService/Controllers/v1/SomethingController.cs b/DoSomethingService/Controllers/v1/SomethingController.cs new file mode 100644 index 0000000..9735852 --- /dev/null +++ b/DoSomethingService/Controllers/v1/SomethingController.cs @@ -0,0 +1,59 @@ +using Microsoft.AspNetCore.Mvc; + +namespace DoSomethingService.Controllers.v1; + +/// +/// This is an api to return some data in a made up senario +/// +[Route("api/v1/[controller]")] +[ApiController] +[ApiVersion("1.0")] +public class SomethingController : ControllerBase +{ + private readonly ILogger _logger; + + /// + /// Setting up the code so it can do things + /// + /// The logger so we know if things have been done + public SomethingController(ILogger logger) + { + _logger = logger; + } + + /// + /// If you need to do something then it will + /// + /// What happened + [HttpGet("DoSomething")] + [ApiExplorerSettings(GroupName ="v1")] + public string Get() + { + return $"You wanted me to do something, there I did something"; + } + + /// + /// If you need to do something then, just tell it + /// what you want to do + /// + /// The thing you want it to do + /// What happened + [HttpGet("WhatSomethingToDo")] + [ApiExplorerSettings(GroupName ="v1")] + public string Get(string whatToDo) + { + return $"You wanted me to {whatToDo}, there I did something"; + } + + /// + /// If you want to post something for it to do + /// + /// The thing you want it to do + /// What happened + [HttpPost("PostSomething")] + [ApiExplorerSettings(GroupName ="v1")] + public string Post(string whatToDo) + { + return $"This was passed to me {whatToDo}, there I did something"; + } +} \ No newline at end of file diff --git a/DoSomethingService/Controllers/v1/SomethingV1Controller.cs b/DoSomethingService/Controllers/v1/SomethingV1Controller.cs deleted file mode 100644 index b09de59..0000000 --- a/DoSomethingService/Controllers/v1/SomethingV1Controller.cs +++ /dev/null @@ -1,44 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace DoSomethingService.Controllers.v1; - -/// -/// -/// -[ApiController] -[Route("v1/something")] -public class SomethingV1Controller : ControllerBase -{ - private readonly ILogger _logger; - - /// - /// - /// - /// - public SomethingV1Controller(ILogger logger) - { - _logger = logger; - } - - /// - /// - /// - /// - /// - [HttpGet("DoSomething")] - public string Get(string input) - { - return $"This was passed to me {input}, there I did something"; - } - - /// - /// - /// - /// - /// - [HttpPost("PostSomething")] - public string Post(string input) - { - return $"This was passed to me {input}, there I did something"; - } -} \ No newline at end of file diff --git a/DoSomethingService/Controllers/v2/SomethingController.cs b/DoSomethingService/Controllers/v2/SomethingController.cs new file mode 100644 index 0000000..a1bbdc4 --- /dev/null +++ b/DoSomethingService/Controllers/v2/SomethingController.cs @@ -0,0 +1,73 @@ +using Microsoft.AspNetCore.Mvc; + +namespace DoSomethingService.Controllers.v2; + +/// +/// This is an api to return some data in a made up senario +/// +[Route("api/v2/[controller]")] +[ApiController] +[ApiVersion("2.0")] +public class SomethingController : ControllerBase +{ + private readonly ILogger _logger; + + /// + /// Setting up the code so it can do things + /// + /// The logger so we know if things have been done + public SomethingController(ILogger logger) + { + _logger = logger; + } + + /// + /// If you need to do something then it will + /// + /// What happened + [HttpGet("DoSomething")] + [ApiExplorerSettings(GroupName ="v2")] + public string Get() + { + return $"You wanted me to do something, there I did something"; + } + + /// + /// If you need to do something then, just tell it + /// what you want to do + /// + /// The thing you want it to do + /// What happened + [HttpGet("WhatSomethingToDo")] + [ApiExplorerSettings(GroupName ="v2")] + public string Get(int whatToDo) + { + return $"You wanted me to {whatToDo}, there I did something"; + } + + /// + /// If you want to post something for it to do + /// + /// The thing you want it to do + /// What happened + [HttpPost("PostSomething")] + [ApiExplorerSettings(GroupName ="v2")] + public string Post(string whatToDo) + { + return $"This was passed to me {whatToDo}, there I did something"; + } + + /// + /// If you need to do something then, just tell it + /// what you want to do + /// + /// The thing you want it to do + /// What happened + [HttpGet("WhatAnotherSomethingToDo")] + [ApiExplorerSettings(GroupName ="v2")] + public string GetAnother(int whatToDo) + { + return $"You wanted me to {whatToDo}, there I did something"; + } + +} \ No newline at end of file diff --git a/DoSomethingService/Controllers/v2/SomethingV2Controller.cs b/DoSomethingService/Controllers/v2/SomethingV2Controller.cs deleted file mode 100644 index 85848b2..0000000 --- a/DoSomethingService/Controllers/v2/SomethingV2Controller.cs +++ /dev/null @@ -1,44 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace DoSomethingService.Controllers.v2; - -/// -/// -/// -[ApiController] -[Route("v2/something")] -public class SomethingV2Controller : ControllerBase -{ - private readonly ILogger _logger; - - /// - /// - /// - /// - public SomethingV2Controller(ILogger logger) - { - _logger = logger; - } - - /// - /// - /// - /// - /// - [HttpGet("DoSomething")] - public string Get(string input, string otherInput) - { - return $"This was passed to me {input}, there I did something then I did {otherInput}"; - } - - /// - /// - /// - /// - /// - [HttpPost("PostSomething")] - public string Post(string input) - { - return $"This was passed to me {input}, there I did something"; - } -} \ No newline at end of file diff --git a/DoSomethingService/DoSomethingService.csproj b/DoSomethingService/DoSomethingService.csproj index b6a656c..9f71894 100644 --- a/DoSomethingService/DoSomethingService.csproj +++ b/DoSomethingService/DoSomethingService.csproj @@ -4,12 +4,20 @@ net7.0 enable enable - true + + + + + + true + $(NoWarn);1591 + + diff --git a/DoSomethingService/Program.cs b/DoSomethingService/Program.cs index 26d6e75..dfe1e2a 100644 --- a/DoSomethingService/Program.cs +++ b/DoSomethingService/Program.cs @@ -1,10 +1,16 @@ using Microsoft.OpenApi.Models; +using Swashbuckle.AspNetCore.SwaggerUI; +using Microsoft.AspNetCore.Mvc.ApplicationModels; +using System.Reflection; + var myAllowSpecificOrigins = "_myAllowSpecificOrigins"; var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddApplicationInsightsTelemetry(); + var apiVersions = new Dictionary(); -apiVersions.Add("v1", "SomethingV1"); -apiVersions.Add("v2", "SomethingV2"); + builder.Services.AddCors(options => { options.AddPolicy(name: myAllowSpecificOrigins, @@ -15,50 +21,82 @@ }); }); -builder.Services.AddControllers(); +builder.Services.AddMvc(c=> { + c.Conventions.Add(new ApiExplorerGroupPerVersionConvention()); +}); -builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); -/*options => +builder.Services.AddSwaggerGen(options => { - /*options.SwaggerDoc("v1", new OpenApiInfo - { - Version = "v1", - Title = "The DoSomething API" - }); - options.SwaggerDoc("v2", new OpenApiInfo - { - Version = "v2", - Title = "The DoSomething API" - });#1# - //options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First()); -});*/ + options.SwaggerDoc("v1", new OpenApiInfo { Title = "My API - V1", Version = "v1" , + Description = "An ASP.NET Core Web API for managing ToDo items", + TermsOfService = new Uri("https://example.com/terms"), + Contact = new OpenApiContact + { + Name = "Example Contact", + Url = new Uri("https://example.com/contact") + }, + License = new OpenApiLicense + { + Name = "Example License", + Url = new Uri("https://example.com/license") + }}); + options.SwaggerDoc("v2", new OpenApiInfo { Title = "My API - V2", Version = "v2", + Description = "An ASP.NET Core Web API for managing ToDo items", + TermsOfService = new Uri("https://example.com/terms"), + Contact = new OpenApiContact + { + Name = "Example Contact", + Url = new Uri("https://example.com/contact") + }, + License = new OpenApiLicense + { + Name = "Example License", + Url = new Uri("https://example.com/license") + } }); + + var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; + options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename)); +}); var app = builder.Build(); app.UseSwagger(); -app.UseSwaggerUI(); - -/*options =>*/ - /*{ - options.SwaggerEndpoint($"/v1/Something/swagger/swagger.json", "SomethingV1"); - options.SwaggerEndpoint($"/swagger/v2/Something/swagger.json", "SomethingV2"); - }*/ - /*options => +app.UseSwaggerUI(options => +{ + app.UseSwaggerUI(options => { - // build a swagger endpoint for each discovered API version - foreach (var description in apiVersions) - { - var name = description.Value.ToUpperInvariant(); - var url = $"/swagger/{description.Key}/Something/swagger.json"; - options.SwaggerEndpoint(url, name); - } - }*/ + options.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); + options.SwaggerEndpoint("/swagger/v2/swagger.json", "My API V2"); + }); + + options.DefaultModelExpandDepth(2); + options.DefaultModelRendering(ModelRendering.Model); + options.DefaultModelsExpandDepth(-1); + options.DisplayOperationId(); + options.DisplayRequestDuration(); + options.DocExpansion(DocExpansion.None); + options.EnableDeepLinking(); + options.EnableFilter(); + options.MaxDisplayedTags(5); + options.ShowExtensions(); + options.ShowCommonExtensions(); + options.EnableValidator(); + options.SupportedSubmitMethods(SubmitMethod.Get, SubmitMethod.Head); + options.UseRequestInterceptor("(request) => { return request; }"); + options.UseResponseInterceptor("(response) => { return response; }"); +}); app.UseHttpsRedirection(); app.UseCors(myAllowSpecificOrigins); -//app.UseAuthorization(); -app.MapControllers().WithOpenApi(); +app.Run(); -app.Run(); \ No newline at end of file +public class ApiExplorerGroupPerVersionConvention : IControllerModelConvention +{ + public void Apply(ControllerModel controller) + { + var controllerNamespace = controller.ControllerType.Namespace; + var apiVersion = controllerNamespace.Split('.').Last().ToLower(); + controller.ApiExplorer.GroupName=apiVersion; + } +} \ No newline at end of file diff --git a/DoSomethingService/appsettings.json b/DoSomethingService/appsettings.json index 10f68b8..c6d2bab 100644 --- a/DoSomethingService/appsettings.json +++ b/DoSomethingService/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ApplicationInsights":{ + "ConnectionString": "InstrumentationKey=6bd44586-aeec-47ae-9056-0019ea08f8f9;IngestionEndpoint=https://centralus-0.in.applicationinsights.azure.com/;LiveEndpoint=https://centralus.livediagnostics.monitor.azure.com/" + } }