Skip to content

Instantly share code, notes, and snippets.

@RickStrahl
Last active October 11, 2021 02:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RickStrahl/9b01d6b217ca469fdab523b6bd9473cd to your computer and use it in GitHub Desktop.
Save RickStrahl/9b01d6b217ca469fdab523b6bd9473cd to your computer and use it in GitHub Desktop.
Minimal API Startup in 6.0 for ASP.NET Core application - Auto-Refresh not working for Razor
"ProductRegistrationService": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"hotReloadEnabled": false,
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
using LicensingService.Configuration;
using Microsoft.AspNetCore.Authentication.Cookies;
using Newtonsoft.Json.Serialization;
using Westwind.Licensing;
using Westwind.Utilities.Data;
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
var configuration = builder.Configuration;
var host = builder.Host;
var config = new ApplicationConfiguration();
configuration.Bind("Application", config);
services.AddSingleton(config);
App.Configuration = config;
var licenseConfig = new LicenseConfiguration();
configuration.Bind("Licensing", licenseConfig);
if (string.IsNullOrEmpty(licenseConfig.ConnectionString))
licenseConfig.ConnectionString = config.LicenseServerConnectionString;
LicenseConfiguration.Current = licenseConfig;
services.AddSingleton(LicenseConfiguration.Current);
services.AddTransient<LicenseSqlDataManager>();
// background startups
Task.Run(() =>
{
var sql = new SqlDataAccess(LicenseConfiguration.Current.ConnectionString);
sql.ExecuteScalar("select top 1 username from users");
});
//services.AddLiveReload(config =>
//{
// config.LiveReloadEnabled = false ;
//});
services.AddAuthentication(options =>
{
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(options => { options.LoginPath = "/administration/login"; });
// for ASP.NET Core 3.x and later, add Runtime Razor Compilation if using anything Razor
services.AddRazorPages(options =>
{
options.Conventions.AuthorizeFolder("/administration");
options.Conventions.AllowAnonymousToPage("/administration/login");
});
//.AddRazorRuntimeCompilation();
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddControllers()
// Use classic JSON
.AddNewtonsoftJson(opt =>
{
var resolver = opt.SerializerSettings.ContractResolver;
if (resolver != null)
{
var res = resolver as DefaultContractResolver;
res.NamingStrategy = new CamelCaseNamingStrategy();
}
opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
if ( builder.Environment.IsDevelopment())
opt.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/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.UseAuthorization();
app.MapRazorPages();
app.Run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment