Skip to content

Instantly share code, notes, and snippets.

View gavilanch's full-sized avatar

Felipe Gavilán gavilanch

  • República Dominicana
View GitHub Profile
"ConnectionStrings": {
"PlaceHolderConnection": "Data Source=.;Initial Catalog={dbName};Integrated Security=True",
}
...
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")
));
[HttpGet("{id}")]
public async Task<ActionResult<Person>> Get(int id)
{
var person = await context.People.Include(x => x.MoviesActors).ThenInclude(x => x.Movie).FirstOrDefaultAsync(x => x.Id == id);
if (person == null) { return NotFound(); }
return person;
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
// Unnecessary code removed for brevity
namespace WebApiSwaggerVersion
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(config =>
// Version 1
namespace WebApiSwaggerVersion.Controllers.V1
{
[ApiController]
[Route("api/v1/[controller]")]
public class WeatherForecastController : ControllerBase
{...}
}
services.AddControllers(options =>
{
options.Conventions.Add(new GroupingByNamespaceConvention());
});
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;
}
}
// Código resumido para ocupar menos espacio
namespace WebApiSwaggerVersion
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(config =>
namespace WebApiSwaggerVersion
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }