asp.net-core-2.0
.NET Core 2 – EF Core Error handling Save changes
Looking through the GitHub issues, there is no DbEntityValidationException equivalent in Entity Framework Core. There’s a blog post (linked from issue #9662 on GitHub), that gives a code example for performing the validation logic yourself, included here for completeness: class MyContext : DbContext { public override int SaveChanges() { var entities = from e in … Read more
The input was not valid .Net Core Web API
Don’t use FromBody. You’re submitting as x-www-form-urlencoded (i.e. standard HTML form post). The FromBody attribute is for JSON/XML. You cannot handle both standard form submits and JSON/XML request bodies from the same action. If you need to request the action both ways, you’ll need two separate endpoints, one with the param decorated with FromBody and … Read more
Local user account store for Web API in ASP.NET Core 2.0
Try start your project in the console with the command dotnet new webapi -au Individual You can open your project in VS after that. (to work around the dialog). Then you can use for example the authorize-attribute. But the project is still configured to use Azure Bearer Authentication. You have to decide where to get … Read more
ASP.NET Core 2 Unable to resolve service for type Microsoft EntityFrameworkCore DbContext
StudentService expects DbContext but the container does not know how to resolve it based on your current startup. You would need to either explicitly add the context to the service collection Startup services.AddScoped<DbContext, SchoolContext>(); services.AddScoped<IStudentService, StudentService>(); Or update the StudentService constructor to explicitly expect a type the container knows how to resolve. StudentService public StudentService(SchoolContext … Read more
ASP.NET Core 1.0 ConfigurationBuilder().AddJsonFile(“appsettings.json”); not finding file
public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile(“appsettings.json”); Configuration = builder.Build(); } This seems to do the trick. However unsure this is the proper way to do it. Kinda feels like a hack.
The specified framework ‘Microsoft.AspNetCore.App’, version ‘2.1.0’ was not found
.NET Core 2.1 SDK will be released this week. If you can’t wait until then, add this to your *.csproj <Project Sdk=”Microsoft.NET.Sdk.Web”> <PropertyGroup> <TargetFramework>netcoreapp2.1</TargetFramework> <RestoreAdditionalProjectSources> https://dotnetfeed.blob.core.windows.net/orchestrated-release-2-1/20180515-07/final/index.json </RestoreAdditionalProjectSources> </PropertyGroup> …. </Project> And download the final SDK from: https://dotnetcli.blob.core.windows.net/dotnet/Sdk/2.1.300/dotnet-sdk-2.1.300-win-x64.exe For more details visit: https://github.com/aspnet/Home/wiki/2.1.0-Early-Access-Downloads
Json Format for a TimeSpan that can be bound using Microsoft.Extensions.Configuration
Timespan format in .net core is D.HH:mm:nn (so “1.02:03:04” is 1 day, 2 hours, 3 mins, 4 seconds). javascript wont be able to read that (we use a custom JsonConverter for timespan objects for that reason), but .Net can. {“timespan”:”1.02:03:04″}
How to unique identify each request in a ASP.NET Core 2 application (for logging)
ASP.NET Core also has HttpContext.TraceIdentifier which uniquely identifies the current request. It is generated for each incoming request by Kestrel, so isn’t suitable for correlating calls to one service from another, but is suitable for correlating logs for a single request together, or to correlating an error message that might be displayed to the user … Read more