ASP.NET Embedded Resource

You can compile binary or text files into the application assembly – here’s the basic idea. Add the file to the project in Visual Studio, and in the Properties, mark it as “Embedded Resource”. The name of the resource is the default namespace for the project + relative namespace (folder structure) + filename. This is […]

Versioned content in MVC

When you add a script or stylesheet to your HTML page, those requests can be cached by the browser, potentially providing outdated content to the browser. If you’re not using a bundler or anything fancy like that, then the only way to prevent this problem is to create a brand new URL whenever your script […]

Non-buffered Output in ASP.NET

Most of the time, it makes sense to build a complete response and return it all at once. Sometimes, if you have a big result, like a large report, you’ll want to send the file a response a little at a time, to keep from storing a huge item in memory. You can write to […]

ASP.NET JSON Self Referencing Properties

In object-oriented languages, an object can hold a reference to itself in a property, and there’s no problem with that. However, when you go to serialize that object, you run into issues. An object with a self-referencing property that loops back to itself would cause an infinite loop and crash when trying to serialize it. […]

JSON casing in ASP.NET Core

Some mallethead decided that they wanted to take UpperCamelCase names of properties and change them to lowerCamelCase when sending JSON to the client. Sure, maybe this fits standard naming conventions, but it means that you’ll end up with different property names. public JsonResult GetFoo() { return Json(new Foo { Name = “Jane Doe”, Age = […]

Exception logging in ASP.NET Core

public void HandleException(IApplicationBuilder app){ app.Run(async requestContext => { try { string path = requestContext.Request.Path; var exceptionHandlerPathFeature = requestContext.Features.Get<IExceptionHandlerPathFeature>(); var exception = exceptionHandlerPathFeature?.Error; User? user = null; try { user = /* implementation */ Cast requestContext.User into your custom user type } catch (Exception ex) { Debug.WriteLine(ex); } if (exception != null) { new Thread(() => […]