-
August 21st, 2022, 11:24 PM
#1
Merge Program.cs and Startup.cs .NET 6
Hi Guys,
I host my asp.net core website on Asphostportal. I'm trying to see if I can merge my old projects Program.cs and Startup.cs files into just the Program.cs file. I upgraded the application to .NET 6, and having them separate works, but I'm trying to really learn what the difference is like.
-- Startup.cs
HTML Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.EntityFrameworkCore;
using IssueTracker2020.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using IssueTracker2020.Models;
using IssueTracker2020.Services;
using IssueTracker2020.Utilities;
using Microsoft.AspNetCore.Identity.UI.Services;
namespace IssueTracker2020
{
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.AddMvc().AddRazorPagesOptions(options =>
//{
// options.Conventions.AddPageRoute("/Pages/Account/Login", "");
//});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(
DataHelper.GetConnectionString(Configuration)));
services.AddIdentity<BTUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = false)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI()
.AddDefaultTokenProviders();
services.AddScoped<IBTRolesService, BTRolesService>();
services.AddScoped<IBTProjectService, BTProjectService>();
services.AddScoped<IBTHistoryService, BTHistoryService>();
services.AddScoped<IBTAccessService, BTAccessService>();
services.AddScoped<INotificationService, NotificatonService>();
services.AddScoped<IBTFileService, BTFileService>();
services.AddScoped<ITicketService, TicketService>();
// Email Register
services.Configure<MailSettings>(Configuration.GetSection("MailSettings"));
services.AddTransient<IEmailSender, EmailService>();
services.AddControllersWithViews();
services.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
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.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=LandingPage}/{id?}");
endpoints.MapRazorPages();
});
}
}
}
-- Program.cs
HTML Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IssueTracker2020.Data;
using IssueTracker2020.Models;
using IssueTracker2020.Utilities;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace IssueTracker2020
{
public class Program
{
public async static Task Main(string[] args)
{
//CreateHostBuilder(args).Build().Run();
var host = CreateHostBuilder(args).Build();
await DataHelper.ManageData(host);
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
var loggerFactory = services.GetRequiredService<ILoggerFactory>();
try
{
var context = services.GetRequiredService<ApplicationDbContext>();
var userManager = services.GetRequiredService<UserManager<BTUser>>();
var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
await ContextSeed.RunSeedMethodsAsync(roleManager, userManager, context);
}
catch (Exception ex)
{
var logger = loggerFactory.CreateLogger<Program>();
logger.LogError(ex, "An error occurred seeding the DB.");
}
}
host.Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.CaptureStartupErrors(true);
webBuilder.UseSetting(WebHostDefaults.DetailedErrorsKey, "true");
webBuilder.UseStartup<Startup>();
});
}
}
Thank you
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|