CopyPastor

Detecting plagiarism made easy.

Score: 1.849772036075592; Reported for: String similarity, Exact paragraph match Open both answers

Possible Plagiarism

Reposted on 2021-11-15
by Tupac

Original Post

Original - Posted on 2021-10-20
by Tupac



            
Present in both answers; Present only in the new answer; Present only in the old answer;

I wrote a demo before, you can refer to it, maybe it is what you want.
Version used:
ASP.NET Core 3.1 Swashbuckle.AspNetCore: 5.4.1
First I created 2 versions of folders and controllers.Therefore, the namespace of each controller corresponds to its folder, like this:
**V1 version**
namespace WebApplication129.Controllers.V1 { [ApiController] [Route("api/v1/[controller]")] public class HomeController : ControllerBase { [Route("test")] [HttpGet] public string Test() { return "v1 test"; } } }
**V2 version**
namespace WebApplication129.Controllers.V2 { [ApiController] [Route("api/v2/[controller]")] public class HomeController : ControllerBase { [Route("test")] [HttpGet] public string Test() { return "v2 test"; } } }
Then create an agreement to inform Swagger, in this way, we can control how Swagger generates Swagger documents, thereby controlling the UI.
Create the following class:
public class GroupingByNamespaceConvention : IControllerModelConvention { public void Apply(ControllerModel controller) { var controllerNamespace = controller.ControllerType.Namespace; var apiVersion = controllerNamespace.Split(".").Last().ToLower(); if (!apiVersion.StartsWith("v")) { apiVersion = "v1"; } controller.ApiExplorer.GroupName = apiVersion; } }
What we do is group controllers according to the last segment of their namespace. Thus, the controllers whose namespace ends in v1 will be grouped under the name “v1“, those that end in v2 are grouped as “v2“, etc.
Now we must apply the convention. For that we go to AddControllers in ConfigureServices and add the convention:
services.AddControllers(options => { options.Conventions.Add(new GroupingByNamespaceConvention()); });
The final complete startup.cs configuration is as follows:
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using System; using WebApplication129.Controllers.conf; namespace WebApplication129 { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(options => { options.Conventions.Add(new GroupingByNamespaceConvention()); }); services.AddSwaggerGen(config => { var titleBase = "Test API"; var description = "This is a Web API for Test operations"; var TermsOfService = new Uri("https://xxxxxx"); var License = new OpenApiLicense() { Name = "Test" }; var Contact = new OpenApiContact() { Name = "Test", Email = "Test@hotmail.com", Url = new Uri("https://xxxxxx") }; config.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = titleBase + " v1", Description = description, TermsOfService = TermsOfService, License = License, Contact = Contact }); config.SwaggerDoc("v2", new OpenApiInfo { Version = "v2", Title = titleBase + " v2", Description = description, TermsOfService = TermsOfService, License = License, Contact = Contact }); }); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseSwagger(); app.UseSwaggerUI(config => { config.SwaggerEndpoint("/swagger/v1/swagger.json", "v1"); config.SwaggerEndpoint("/swagger/v2/swagger.json", "v2"); }); app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } }
Visit url: `https://localhost:yourport/swagger/index.html`

[![enter image description here][1]][1]

[1]: https://i.stack.imgur.com/JJb2r.gif
According to your request, is it only showing some APIs of v2 when switching the V2 version?
You can do this:
**Versions Used**
ASP.NET Core 3.1 Swashbuckle.AspNetCore: 5.4.1
First I created 2 versions of folders and controllers:
[![enter image description here][1]][1]

Therefore, the namespace of each controller corresponds to its folder, like this:
**V1 version**

namespace WebApplication129.Controllers.V1 { [ApiController] [Route("api/v1/[controller]")] public class HomeController : ControllerBase { [Route("test")] [HttpGet] public string Test() { return "v1 test"; } } } **V2 version** namespace WebApplication129.Controllers.V2 { [ApiController] [Route("api/v2/[controller]")] public class HomeController : ControllerBase { [Route("test")] [HttpGet] public string Test() { return "v2 test"; } } }
Then create an agreement to inform Swagger, in this way, we can control how Swagger generates Swagger documents, thereby controlling the UI.
Create the following class:
public class GroupingByNamespaceConvention : IControllerModelConvention { public void Apply(ControllerModel controller) { var controllerNamespace = controller.ControllerType.Namespace; var apiVersion = controllerNamespace.Split(".").Last().ToLower(); if (!apiVersion.StartsWith("v")) { apiVersion = "v1"; } controller.ApiExplorer.GroupName = apiVersion; } }
What we do is group controllers according to the last segment of their namespace. Thus, the controllers whose namespace ends in v1 will be grouped under the name “v1“, those that end in v2 are grouped as “v2“, etc.
Now we must apply the convention. For that we go to AddControllers in ConfigureServices and add the convention:
services.AddControllers(options => { options.Conventions.Add(new GroupingByNamespaceConvention()); });

The final complete startup.cs configuration is as follows:
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using System; using WebApplication129.Controllers.conf; namespace WebApplication129 { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(options => { options.Conventions.Add(new GroupingByNamespaceConvention()); }); services.AddSwaggerGen(config => { var titleBase = "Test API"; var description = "This is a Web API for Test operations"; var TermsOfService = new Uri("https://xxxxxx"); var License = new OpenApiLicense() { Name = "Test" }; var Contact = new OpenApiContact() { Name = "Test", Email = "Test@hotmail.com", Url = new Uri("https://xxxxxx") }; config.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = titleBase + " v1", Description = description, TermsOfService = TermsOfService, License = License, Contact = Contact }); config.SwaggerDoc("v2", new OpenApiInfo { Version = "v2", Title = titleBase + " v2", Description = description, TermsOfService = TermsOfService, License = License, Contact = Contact }); }); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseSwagger(); app.UseSwaggerUI(config => { config.SwaggerEndpoint("/swagger/v1/swagger.json", "v1"); config.SwaggerEndpoint("/swagger/v2/swagger.json", "v2"); }); app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } }
Visit url: `https://localhost:yourport/swagger/index.html`
Result:
[![enter image description here][2]][2]

[1]: https://i.stack.imgur.com/NS6Er.png [2]: https://i.stack.imgur.com/EKmoA.gif

        
Present in both answers; Present only in the new answer; Present only in the old answer;