ASP.NET Core v2 (2015) MVC : How to get raw JSON bound to a string without a type?

The cleanest option I’ve found is adding your own simple InputFormatter: public class RawJsonBodyInputFormatter : InputFormatter { public RawJsonBodyInputFormatter() { this.SupportedMediaTypes.Add(“application/json”); } public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context) { var request = context.HttpContext.Request; using (var reader = new StreamReader(request.Body)) { var content = await reader.ReadToEndAsync(); return await InputFormatterResult.SuccessAsync(content); } } protected override bool CanReadType(Type type) … Read more

ASP.NET Core 2.0 disable automatic challenge

As pointed out by some of the other answers, there is no longer a setting to turn off automatic challenge with cookie authentication. The solution is to override OnRedirectToLogin: services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.Events.OnRedirectToLogin = context => { context.Response.Headers[“Location”] = context.RedirectUri; context.Response.StatusCode = 401; return Task.CompletedTask; }; }); This may change in the future:

Using EF Core ThenInclude() on Junction tables

but I get an ICollection in the last lambda displayed, so I obviously need the Select() No, you don’t. EF Core Include / ThenInclude totally replace the need of Select / SelectMany used in EF6. Both they have separate overloads for collection and reference type navigation properties. If you use the overload with collection, ThenInclude … Read more

Change the JSON serialization settings of a single ASP.NET Core controller

ASP.NET Core 3.0+ You can achieve this with a combination of an Action Filter and an Output Formatter. Things look a little different for 3.0+, where the default JSON-formatters for 3.0+ are based on System.Text.Json. At the time of writing, these don’t have built-in support for a snake-case naming strategy. However, if you’re using Json.NET … Read more

Creating a proxy to another web api with core

If anyone is interested, I took the Microsoft.AspNetCore.Proxy code and made it a little better with middleware. Check it out here: NuGet here: Microsoft archived the other one mentioned in this post, and I plan on responding to any issues on this project. Basically, it makes reverse proxying another web server a lot … Read more

Custom Authentication in ASP.Net-Core

From what I learned after several days of research, Here is the Guide for ASP .Net Core MVC 2.x Custom User Authentication In Startup.cs : Add below lines to ConfigureServices method : public void ConfigureServices(IServiceCollection services) { services.AddAuthentication( CookieAuthenticationDefaults.AuthenticationScheme ).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options => { options.LoginPath = “/Account/Login”; options.LogoutPath = “/Account/Logout”; }); services.AddMvc(); // authentication services.AddAuthentication(options => … Read more

View POST request body in Application Insights

You can simply implement your own Telemetry Initializer: For example, below an implementation that extracts the payload and adds it as a custom dimension of the request telemetry: public class RequestBodyInitializer : ITelemetryInitializer { public void Initialize(ITelemetry telemetry) { var requestTelemetry = telemetry as RequestTelemetry; if (requestTelemetry != null && (requestTelemetry.HttpMethod == HttpMethod.Post.ToString() || requestTelemetry.HttpMethod … Read more

Unable to create migrations after upgrading to ASP.NET Core 2.0

You can add a class that implements IDesignTimeDbContextFactory inside of your Web project. Here is the sample code: public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<CodingBlastDbContext> { public CodingBlastDbContext CreateDbContext(string[] args) { IConfigurationRoot configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile(“appsettings.json”) .Build(); var builder = new DbContextOptionsBuilder<CodingBlastDbContext>(); var connectionString = configuration.GetConnectionString(“DefaultConnection”); builder.UseSqlServer(connectionString); return new CodingBlastDbContext(builder.Options); } } Then, navigate to … Read more

.NET Core MVC Page Not Refreshing After Changes

In ASP.NET Core 3.0 and higher, RazorViewEngineOptions.AllowRecompilingViewsOnFileChange is not available. Surprised that refreshing a view while the app is running did not work, I discovered the following solution: Add Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package to the project Add the following in Startup.cs: services.AddControllersWithViews().AddRazorRuntimeCompilation(); Here’s the full explanation for the curious.