Edi Wang

.NET and Azure Developer

.NET C# Programming Language and .NET Platform

Auto Refresh Settings Changes in ASP.NET Core Runtime

In ASP.NET Core, if you modify the settings in appsettings.json, you will have to restart the site to take effect. Is there a way to refresh and apply it automatically after you modify the settings? Background Let's take a look at a website created by ASP.NET Core default templates. It includes two settings files: appsettings.json appsettings.Development.json The former one is used in production …
.NET ASP.NET

Dependency Injection with Multiple Implementations in ASP.NET Core

The built-in Dependency Injection (DI) in ASP.NET Core is very useful, but how do you deal with an interface has multiple implementations? Can the runtime choose one of these implementations based on configuration? Is there a way to get rid of reflection? Let me show you how to dynamically select a specific implementation of an interface at run time according to the configuration file without …
.NET ASP.NET Design Pattern Dependency Injection

Path Caveat with ASP.NET Core 2.2 IIS Hosting

ASP.NET Core 2.2 has been in place for some time, with a new feature that can use the new AspNetCoreModuleV2 and deploy with InProcess mode on IIS to dramatically improve performance. These days Azure App Service finally completed the deployment of this new version of the module, I configured my blog to the new module, and it exploded in production. Let's see why and how to solve it. If you don't …
.NET ASP.NET IIS

.NET Core WebUtility.UrlEncode or HttpUtility.UrlEncode

When we are dealing with URL encode in .NET Core 2.1, there are two APIs: System.Net.WebUtility.UrlEncode and System.Web.HttpUtility.UrlEncode. What's the difference between them? And which one should we prefer to use? I have done some research today, here's my findings. 1. Test Results First, let's see some tests. I've tested 2 couples of the same method between WebUtility class and HttpUtility …
.NET URL Encoding

Migrating Old ASP.NET Applications to .NET Core

More and more people are talking about .NET Core these days. It is true that .NET Core is the future, and .NET Framework will still be maintained because very large number of applications can’t be migrated in a short time. .NET Core and .NET Framework are just like electric cars and gasoline powered cars. Gasoline cars is mature, and you can drive it without any problem, but electric cars have …
.NET ASP.NET

Generate Captcha Code in ASP.NET Core

If you want to use captcha code to protect your website from spam messages, there are a few options such as Google ReCaptcha and captcha.com. Both of them can be integrated into ASP.NET Core applications. However, you may still want to generate the captcha code yourself for some reason, such as your website may be used in mainland China... This post will show you how to generate and use captcha …
.NET ASP.NET

Add Watermark to Uploaded Image in ASP.NET Core

Adding watermark to an image is very widely used in websites in order to protect the content owner's copyright, such as a blog system like this website. In traditional ASP.NET (.NET Framework), we could use System.Web.Helpers.WebImage to add text watermark like this: var image = new WebImage(imageBytes); image.AddTextWatermark( Settings.Instance.WatermarkText, "White", Settings.Instance. …
.NET ASP.NET

Get App Version in .NET Core

We can define versions on our .NET Core applications, but how to get the version at runtime? There are several ways to do it. First, let's see a typical .NET Core project file with versioning. We can define the version numbers for AssemblyVersion, FileVersion and Version in the project file (.csproj). The AssemblyVersion and FileVersion attributes must be in format of "major[.minor[.build[. …
.NET

Send AntiForgeryToken via jQuery Ajax in ASP.NET Core

In ASP.NET Core, if we use jQuery Ajax to post data to the server, and we want the ValidateAntiForgeryToken attribute to work. We have to do some tricks. The official document didn't document how to do it via jQuery. Let me show you how to do it. Please do read the official document first: https://docs.microsoft.com/en-us/aspnet/core/security/anti-request-forgery?view=aspnetcore-2.1  In my …
.NET AJAX ASP.NET jQuery

Prevent Image Hotlinking in ASP.NET/Core Applications

Introduction As a website developer, we sometimes don't want the images on our own website to be directly referenced and showed on other's website. It can cause a lot network bandwidth for our datacenters in some cases, which means costing money for us to pay for the one who use our images. For example, your website is A.com, you have an image on http://a.com/facepalm.jpg and B.com used your …
.NET ASP.NET IIS