mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2025-12-06 05:32:03 +01:00
88 lines
2.7 KiB
C#
88 lines
2.7 KiB
C#
// Section 1 - import namespaces
|
|
using Microsoft.AspNetCore.Identity; // IdentityUser
|
|
using Microsoft.EntityFrameworkCore; // UseSqlServer, UseSqlite
|
|
using Northwind.Mvc.Data; // ApplicationDbContext
|
|
using Packt.Shared; // AddNorthwindContext extension method
|
|
using System.Net.Http.Headers; // MediaTypeWithQualityHeaderValue
|
|
|
|
// Section 2 - configure the host web server including services
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
|
|
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
|
options.UseSqlServer(connectionString));
|
|
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
|
|
|
|
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
|
|
.AddRoles<IdentityRole>() // enable role management
|
|
.AddEntityFrameworkStores<ApplicationDbContext>();
|
|
builder.Services.AddControllersWithViews();
|
|
|
|
/*
|
|
// if you are using SQL Server
|
|
string? sqlServerConnection = builder.Configuration
|
|
.GetConnectionString("NorthwindConnection");
|
|
|
|
if (sqlServerConnection is null)
|
|
{
|
|
Console.WriteLine("SQL Server database connection string is missing!");
|
|
}
|
|
else
|
|
{
|
|
builder.Services.AddNorthwindContext(sqlServerConnection);
|
|
}
|
|
*/
|
|
|
|
// if you are using SQLite default is ..\Northwind.db
|
|
builder.Services.AddNorthwindContext();
|
|
|
|
builder.Services.AddHttpClient(name: "Northwind.WebApi",
|
|
configureClient: options =>
|
|
{
|
|
options.BaseAddress = new Uri("https://localhost:5002/");
|
|
options.DefaultRequestHeaders.Accept.Add(
|
|
new MediaTypeWithQualityHeaderValue(
|
|
mediaType: "application/json", quality: 1.0));
|
|
});
|
|
|
|
builder.Services.AddHttpClient(name: "Minimal.WebApi",
|
|
configureClient: options =>
|
|
{
|
|
options.BaseAddress = new Uri("https://localhost:5003/");
|
|
options.DefaultRequestHeaders.Accept.Add(
|
|
new MediaTypeWithQualityHeaderValue(
|
|
"application/json", 1.0));
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
// Section 3 -
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseMigrationsEndPoint();
|
|
}
|
|
else
|
|
{
|
|
app.UseExceptionHandler("/Home/Error");
|
|
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
|
app.UseHsts();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
app.UseStaticFiles();
|
|
|
|
app.UseRouting();
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllerRoute(
|
|
name: "default",
|
|
pattern: "{controller=Home}/{action=Index}/{id?}");
|
|
app.MapRazorPages();
|
|
|
|
// Section 4 - start the host web server listening for HTTP requests
|
|
app.Run();
|