.NET Developer, Microsoft MVP

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


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

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

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

.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 = 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 1. Undo the Ink We need a few A...

Stack UWP InkCanvas

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

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

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 GetDataAsync(string searchTerm, int pageIndex, bool includePreRelease = false) { try { IDictionary queryOptions = new Dictionary { { "filter",...

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

我在《上海轨道交通》应用中是如何解析XML数据的

本文针对.NET初学者介绍LINQ TO XML,你会看见很多为了通俗易懂而故意描述得不专业的语句,所以高手勿入!本文介绍的方法不只针对WP,其他任何.NET项目也可以参考。 我在Windows Phone平台上发布的《上海轨道交通》使用的是离线XML作为数据存储。好处是不需要安装任何三方库,.NET自己就有能力解析。如果用JSON装逼还得下载JSON.NET呢。又因为都是只读数据,也用不着为了装逼而用SQL Lite或SQL CE什么的数据库杀鸡用牛刀。 下面介绍一下如何在工程里内嵌XML文件并解析到对象的一般方法。 《上海轨道交通》的XML文件保存的是所有的站点信息,单个节点看起来就像这样:...

LINQ XML Shanghai Metro

如何把基于事件的异步方法包装成基于Task的async方法

自从有了.NET4.5,我们又多了一个装逼语法:async/await,可以写出优越感然后进一步鄙视Java。然而,有时候一些老的API只提供了基于事件的异步方法,没有提供返回Task的异步方法,影响我们的逼格,怎么办呢?就像Windows Phone 8的webclient,只提供了基于事件的DownloadStringAsync方法,写的时候就像这样:var client = new WebClient(); client.DownloadStringCompleted += (s, e) = { ... }; client.DownloadStringAsync(...);为了保住我们的逼格,大微软早就为我们提供了解决方法:TaskCompletionSource,非常适合把基于事件的异步封装成Task方法。这个类型返回是一个可以被await的Task,事件callbac...

.NET async await