Category: C#

Block access to images from other websites

In ASP.NET you can write HTTP Modules to perform actions inside the request pipeline before the request is being finally processed. In this example we use an HTTP Module to check whether the REFERER header matches our website. If it doesn’t then we won’t allow access to the resources. By doing this we can simply

Running a background thread inside an ASP.NET website

When most people think of a web site or web application they think of something that respond to user navigations. The site has a life cycle that begins when the call comes in and ends when the response has been returned. Between these users calls the site is dead. But in reality it’s possible to

Extend MVC controller to support Toastr messages

In an earlier post I showed how to create an MVC wrapper for Toastr, a JavaScript library used for displaying user messages in the web browser. In the post I mentioned that there are two ways to make it possible to call an AddToastMessage function directly from the action methods in the controller: Implement an

Create an MVC wrapper for Toastr

Toastr is a great little library for creating user messages on your website. It’s written in JavaScript and is as such platform independent. But if you’re like me (mainly working in ASP.NET MVC) then creating a server side extension to get messaging support directly in your controllers, could be something to consider. The code shown

Using Json.NET as default JSON serializer in MVC

Returning JSON from an MVC action method is very easy. Just look at this simple example: public JsonResult GetItemData(int itemId) { // …things to do… var response = new { result = “ok” }; return Json(response); } Serialization of the object into JSON is taken care of by ASP.NET in the background and you can

Database related attributes in Code First

This post is a follow-up on the post about Validation attributes in Code-First. While validation attributes focus on making sure the data in the model is correct, the attributes listed here instead focus on instructing Entity Framework’s database generation engine how the database should be structured. Column Specify what column name to use for this

Validation attributes in Code First

DataAnnotations are really helpful when creating a model in Entity Framework Code-First. They are a simple way of instructing EF how you want the model to be structured and how it should translate the model into a relational database. You can also use validation rules to specify what values are allowed in your parameters. When

When both update-database and add-migrations fail while referring to each other

Today I checked out a project from Git on a new machine. The project was using Entity Framework to connect to a local SQL Server on my developer machine. To install the database I ran update-database in the Package Manager Console and got an error message complaining about “pending changes” and that I should run

Create your own extension methods to use in LINQ

The possibility to create extension methods has been around for some time in the .Net Framework. It basically gives you the possibility to extend a class with new methods without having to create a new class that extends the original one. If the original class is sealed you can’t even extend the class, but you

How to prevent duplicate application instances

Sometimes it’s important to make sure your program isn’t running more than one instance at the time. In C# this is an easy task using the provided .NET libraries. In this short illustrative example the program counts the number of processes with the same assembly name as this current assembly. If any are found, the

%d bloggers like this: