How to enable cors in ASP.NET Core 6.0 Web API project?

Add service builder.Services.AddCors and app add app.UseCors(“corsapp”); replace builder.WithOrigins(“*”) with builder.WithOrigins(“http://localhost:800”, “https://misite.com”); check documentation var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); //services cors builder.Services.AddCors(p => p.AddPolicy(“corsapp”, builder => { builder.WithOrigins(“*”).AllowAnyMethod().AllowAnyHeader(); })); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } … Read more

Adding Autofac to .NET core 6.0 using the new single file template

I found this Microsoft docs var builder = WebApplication.CreateBuilder(args); builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); // Register services directly with Autofac here. // Don’t call builder.Populate(), that happens in AutofacServiceProviderFactory. builder.Host.ConfigureContainer<ContainerBuilder>( builder => builder.RegisterModule(new MyApplicationModule())); var app = builder.Build();

What is AddEndpointsApiExplorer in ASP.NET Core 6

AddEndpointsApiExplorer is for Minimal APIs whereas AddApiExplorer requires, at least, MVC Core. For API projects, AddControllers calls AddApiExplorer on your behalf. But Why Does Everything Still Work With AddEndpointsApiExplorer? With the introduction of Endpoint Routing, everything in the routing system boils down to an Endpoint. ASP.NET Core uses the Application Model, namely ApplicationModel, ControllerModel, and … Read more

Config connection string in .net core 6

Configuration.GetConnectionString(string connName) in .NET6 is under builder: var builder = WebApplication.CreateBuilder(args); string connString = builder.Configuration.GetConnectionString(“DefaultConnection”); also AddDbContext() is under builder.Services: builder.Services.AddDbContext<YourContext>(options => { options.UseSqlServer(builder.Configuration.GetConnectionString(“DefaultConnection”)); });

tech