Query string in .Net core and MVC 6


As you all know that from MVC6 we are seeing some changes in our regular coding techniques.

We were using below lines in MVC5 for getting values from query string:

var myQueryString = Request.QueryString[“myQueryString”]

But in MVC6, above lines will throw errors in your code.

So, you might be wondering how can we achieve this in MVC6.

Below is the way:

We can use Request.Query which is new in ASPNET 5 and MVC6.

var myQueryString = Request.Query;

For example, you have an URL like:


So to get all keys we will write below code:

var myQueryString = Request.Query;

Here myQueryString.keys contains all the keys which are there in your query string.

Now to take those keys out one by one, we will write below code:

foreach(var key in myQueryString.Keys)
   //// Do the operations as per your need with the key

That is it. You can easily get your values from the Query string in MVC6 and .Net core.

MVC6 (.Net core) applications are self hosted : Optional IIS integration

All ASP.NET Core applications are self-hosted.

Yes you read it right!

So does it mean IIS is not required?

Yes, I mean we can host MVC6 application even without IIS.

Wow…but How?

I assume you have already downloaded .Net core from here.

Next step would be to create sample MVC6 project.

For now you can directly clone it from Github by below commands:

git clone git@github.com:aspnet/cli-samples.git
cd cli-samples\HelloMvc

Then we would restore and run the project as below:

dotnet restore
dotnet run 

That is it!

Now navigate to localhost:5000 in your web browser and you can see your application hosted there.

You must be wondering how it happened?

You can check it by yourself in code.

Open Program.cs class and you can see below code there:

public class Program
    public static void Main(string[] args)
        var config = new ConfigurationBuilder()
            .AddEnvironmentVariables(prefix: "ASPNETCORE_")

        var host = new WebHostBuilder()
            .UseIISIntegration() //// Here IIS integration is optional


As you can see in above comments, IIS integration is optional in .Net core.

Happy coding!

WebListener : A Windows HTTP server for ASP.NET Core

Special thanks to Mr.Tom Dykstra from Microsoft for suggesting some points for this post.

Web Listener is now available on Nuget.

First of all, what is Web Listener?

Web Listener is one of the servers of .Net core.

ASP.NET Core is completely decoupled from the web server environment that hosts the application. ASP.NET Core supports hosting in IIS and IIS Express, and self-hosting scenarios using the Kestrel and WebListener HTTP servers.

As per the .Net team says

WebListener is a Windows HTTP server built on the Http.Sys kernel mode driver. It exposes a number of Http.Sys features previously only availabe in IIS.

Fine, but when should we use Web Listener?

If you are on Windows and  if you want to expose your server directly to the internet. Because Kestrel is not certified for direct exposure.

Or you need any of the features listed below:

  • Windows Authentication
  • Port sharing
  • HTTPS with SNI
  • HTTP/2 over TLS (Windows 10)
  • Direct file transmission
  • Response caching

It is also Recommended for edge deployment because it’s built on the existing Http.Sys HTTP stack. Http.Sys is mature technology that protects against many kinds of attacks. Kestrel is newer, hasn’t had a chance to develop as many protective features yet.

NOTE: Please note that it is Not compatible with ASP.NET Core Module, so can’t be used with IIS even if you wanted to.

Example scenario:

  • Can use Kestrel for internal micro service to micro service communications on private network (kestrel faster), should use WebListener for directly externally facing micro service layer.
  • If a cluster is not on a closed private virtual network, should use WebListener for the inter-service communication as they are open to other things contacting them on their “internal” ports.

Great, and what are Supported Windows versions?

  • Windows 7 and Windows Server 2008 R2 and later

So, How to get it via Nuget?

You can get it using below command:


Great, What to do after getting it from Nuget?

You can use it in code as shown below:

public static void Main(string[] args)
            var host = new WebHostBuilder()
                .UseWebListener(options =>
                    options.ListenerSettings.Authentication.Schemes = AuthenticationSchemes.NTLM;
                    options.ListenerSettings.Authentication.AllowAnonymous = false;


Awesome, anything extra needed to use WebListener?

Yes, below are few points I got from Mr.Tom Dykstra:


Happy coding!

JPEG decoder for .NET Core

Some of you might already know that there is “currently” not any built in default JPEG decoder class for .NET Core since System.Drawing (except for System.Drawing.Primitives) is currently not present as a Nuget package.

So is there any way of DECODING JPEG pictures on .NET Core for some server side processing?

Yes certainly there is!

All you need to do is to download a Nuget package called ImageProcessor 2.4.4 which you can find here for full .NET Framework

Is it available for .Net Core?

Yes it is! It is only on MyGet for the time being. That is hosted here.

Any type supported apart from JPEG?

Encoding/decoding of image formats (plugable):
1. Jpeg (Includes Subsampling. Progressive writing required)
2. Bmp (Read: 32bit, 24bit, 16 bit. Write: 32bit, 24bit just now)
3. Png (Read: Rgb, Rgba, Grayscale, Grayscale + alpha, Palette. Write: Rgb, Rgba, Grayscale, 4. Grayscale + alpha, Palette) Needs interlaced decoding #379
5. Gif (Includes animated)

Some of basic samples are as below:

Sample 1:

// resizing and filter (grayscale)
using (FileStream stream = File.OpenRead(“foo.jpg”))
using (FileStream output = File.OpenWrite(“bar.jpg”))
Image image = new Image(stream);
image.Resize(image.Width / 2, image.Height / 2)

Sample 2: Accessing Pixels

Image image = new Image(400, 400);
using (PixelAccessor<Color, uint> pixels = image.Lock())
pixels[200, 200] = Color.White;

You can find more advantages of this Nuget package here.

Thanks James Jackson-South for building this.

Happy coding!

Moq in Asp .Net core

UPDATE on 15th October 2016:

Moq for .Net core is now available on Nuget and you can get it via below command:


It means the setting mentioned below are not required now.

UPDATE on 20th October 2016:

Moq Nuget version 4.6.38-alpha worksfor .Net core- but e.g. 4.5.3 doesn’t work, that complains that Moq 4.5.3 supports net45 and one or more packages are incompatible with .NETCoreApp V1.0.

See this post for more information.

Old Post:

I was messing around a bit for Unit testing in .Net core and I suddenly remembered about mocking, and thus I decided to use Moq which is the most popular .Net mocking framework.

After few attempts I got below error while compiling:

ASP.NET Core 5.0 error CS0246: The type or namespace name ‘Moq’ could not be found (are you missing a using directive or an assembly reference?)

After searching more on this, I wondered that Moq is not supported by .Net core!!!!!

Though I thought it is not possible as Moq is very popular among developers.

After searching more, I found that ASP.NET team from Microsoft have created a special version of Moq for internal usage.

You need to add special source to your Nuget package manager to be able to locate development version of packages by .Net team.

URL of sources where you could find this packet is here

Ok, but how to use it?

There are currently several ways to use it.

First of all Register a Moq dependencies in project.json:

"dependencies": {
        "xunit": "2.2.0-beta2-build3300",
        "dotnet-test-xunit": "2.2.0-preview2-build1029",
        "moq": "4.6.38-alpha"

Now for Nuget, you can perform any of below steps:

  • Configure a new NuGet feed to be able to reference the moq.netcore package
  • You can Create a NuGet.config file in project home directory.

If you have decided to create Nuget.config file then add below line of codes in that:

<?xml version="1.0" encoding="utf-8"?>
            <add key="AspNetVNext" value="https://www.myget.org/F/aspnetcidev/api/v3/index.json" />
            <add key="NuGet" value="https://api.nuget.org/v3/index.json" />

And if you have decided to configure a new Nuget feed then make sure configuration matches with below configurations:


That is it!

Now you can use Moq with Asp .Net core.

Happy coding!