How to get the Development/Staging/production Hosting Environment in ConfigureServices

You can easily access it in ConfigureServices, just persist it to a property during Startup method which is called first and gets it passed in, then you can access the property from ConfigureServices. public Startup(IWebHostEnvironment env, IApplicationEnvironment appEnv) { …your code here… CurrentEnvironment = env; } private IWebHostEnvironment CurrentEnvironment{ get; set; } public void ConfigureServices(IServiceCollection … Read more

Entity Framework Core: DbContextOptionsBuilder does not contain a definition for ‘usesqlserver’ and no extension method ‘usesqlserver’

First we install the Microsoft.EntityFrameworkCore.SqlServer NuGet Package: PM > Install-Package Microsoft.EntityFrameworkCore.SqlServer Then, after importing the namespace with using Microsoft.EntityFrameworkCore; we add the database context: services.AddDbContext<AspDbContext>(options => options.UseSqlServer(config.GetConnectionString(“optimumDB”)));

How to specify the port an ASP.NET Core application is hosted on?

In ASP.NET Core 3.1, there are 4 main ways to specify a custom port: Using command line arguments, by starting your .NET application with –urls=[url]: dotnet run –urls=http://localhost:5001/ Using appsettings.json, by adding a Urls node: { “Urls”: “http://localhost:5001” } Using environment variables, with ASPNETCORE_URLS=http://localhost:5001/. Using UseUrls(), if you prefer doing it programmatically: public static class … Read more

How to set up Automapper in ASP.NET Core

I figured it out! Here’s the details: Add the main AutoMapper Package to your solution via NuGet. Add the AutoMapper Dependency Injection Package to your solution via NuGet. Create a new class for a mapping profile. (I made a class in the main solution directory called MappingProfile.cs and add the following code.) I’ll use a … Read more

How to unapply a migration in ASP.NET Core with EF Core

Use: CLI > dotnet ef database update <previous-migration-name> Package Manager Console PM> Update-Database <previous-migration-name> Example: PM> Update-Database MyInitialMigration Then try to remove last migration. Removing migration without database update doesn’t work because you applied changes to database. If using PMC, Try: PM> update-database 0 This will wipe the database and allow you to remove the … Read more

ASP.NET Core Web API exception handling

Quick and Easy Exception Handling Simply add this middleware before ASP.NET routing into your middleware registrations. app.UseExceptionHandler(c => c.Run(async context => { var exception = context.Features .Get<IExceptionHandlerPathFeature>() .Error; var response = new { error = exception.Message }; await context.Response.WriteAsJsonAsync(response); })); app.UseMvc(); // or .UseRouting() or .UseEndpoints() Done! Enable Dependency Injection for logging and other purposes … Read more

How to read AppSettings values from a .json file in ASP.NET Core

This has had a few twists and turns. I’ve modified this answer to be up to date with ASP.NET Core 2.0 (as of 26/02/2018). This is mostly taken from the official documentation: To work with settings in your ASP.NET application, it is recommended that you only instantiate a Configuration in your application’s Startup class. Then, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)