.NET Developer, Microsoft MVP

C# and .NET 63 C# Programming Language and .NET Platform


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. …

ASP.NET .NET Core

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 " …

.NET Core

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. …

AJAX ASP.NET jQuery .NET Core

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 …

ASP.NET IIS .NET Core

Use ICMP Ping in .NET Core 2.0

In classic .NET Framework applications, we have used System.Net.NetworkInformation.Ping class to Ping a host address. This API is not included by default in .NET Core, even in version 2.0. Here is how to do it in a .NET Core way. First, we need to reference a package System.Net.Ping Install-Package System.Net.Ping This will give us the same API set as .NET Framework. Then, in your .NET …

.NET Core Ping ICMP

Get Client IP Address in ASP.NET Core 2.x

In classic ASP.NET we used to get client IP Address by Request.UserHostAddress. But this does not apply to ASP.NET Core 2.0. We need a different way to retrieve HTTP Request information. 1. Define a variable in your MVC controller private IHttpContextAccessor _accessor; 2.  DI into the controller's constructor public SomeController(IHttpContextAccessor accessor) { _accessor = …

ASP.NET .NET Core

Windows 10 UWP: Undo / Redo on InkCanvas

The InkCanvas in UWP only got pens by default, it can not perform Undo or Redo. To implement this, we will need to code for ourselves. Official document covered Undo functionalilty, but not redo. Today, I have successfully done it, and I'd like to share with you. First, you need to add two custom buttons on the InkToolbar for Undo / Redo <InkToolbarCustomToolButton Name="undoButton" …

Stack UWP InkCanvas

如何高逼格读取Web.config中的AppSettings

先插句题外话,下版本的ASP.NET貌似把web.config撸掉了,都变成json了。所以本文讨论的内容可能以后用不到了,但是一些设计思想还是可以用的~ 直接进入正题,在ASP.NET网站里(也包括其他有web.config, app.config)的.NET工程里,读AppSettings的值是个很常见的场景。比如: <add key="EnableAzureWebTrace" value="true"/> 在代码里读的时候就会用到: ConfigurationManager.AppSettings["EnableAzureWebTrace"]; 这个[]索引器返回的是string类型。所以下一步我们通常需要类型转换才能在代码里拿来用。比如这个例子里,我们就要转换成bool。其他时候,可能要转换为int等类型。 string …

config

Portable Class Library中如何调用WCF OData Service

上个月发布了一个WP8.1应用:NuGet Search,并在GitHub开了源:https://github.com/EdiWang/WP-NuGetSearch NuGet的服务接口是个WCF OData Service,为了装逼,我尝试使用了跨Framework的PCI工程,结果no zuo no die了。和一般.NET类库不同,PCL有些tricky的地方要爆。下面是开荒成功的代码: public async Task<Response<IEnumerable<V2FeedPackage>>> GetDataAsync(string searchTerm, int pageIndex, bool includePreRelease = false) { try { IDictionary& …

WCF PCL OData

简单粗暴有效解释ASP.NET中的线程池是怎样处理Http请求的

自从有了.NET 4.5,我们又多了一个装逼语法:async,await。但如果错用就会装逼不成反变傻逼。首先我们得明白在ASP.NET中async await所针对的问题,这样才能正确的装逼。于是我们就不得不先研究一下线程池。 在IIS服务器上,处理Http请求的是线程,和Windows的其他软件一样,干活的永远是线程,而不应该说是进程。一个线程同时只能处理一个request,而web上的request不可能同时永远只有一个,所以线程需要和他的小伙伴们一起组成线程池,才能保证网站的响应。当一个线程处理完了手头的请求,它就被释放掉了,于是如果再有新的请求进来他就能再去处理。但如果当线程用完了,并且他们正在处理的请求都没完成,网站就卡住了,用户就只能等出翔。这时候IIS就会返回一个HTTP 503爆给用户。 打个比方,IIS服务器就好像银行,Http请求就好像顾客,银行开的窗口数量就是进 …

IIS async await Thread