Query string in .Net core and MVC 6

download

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:

http://www.myWebsite.com/Home/Index?myQueryString1=abc&myQueryString2=xyz

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()
            .AddCommandLine(args)
            .AddEnvironmentVariables(prefix: "ASPNETCORE_")
            .Build();

        var host = new WebHostBuilder()
            .UseConfiguration(config)
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration() //// Here IIS integration is optional
            .UseStartup()
            .Build();

        host.Run();
    }
}

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

Happy coding!

JSON with camel case in MVC

70fdb0e6-22bd-11e6-82c0-9eee1813955a

 

Yes, the wait is over.

I have heard it manytimes why MVC does not support Camel Case serialization. As workaround people used to use ServiceStack or Json.NET.

But as per recent announcement, MVC 1.0.0 now serializes JSON with camel case names by default.

How?

Before it was something like below:

public class Person
{
  public int Id { get; set; }
  public string FullName { get; set; }
}

Would serialize to:

{“Id”:1,”Name”:”Neel Bhatt”}

Now after changes, it would automatically convert it to camel case as below:

{“id”:1,”fullName”:”Neel Bhatt”} //// Not the first letter here

How to add in MVC?

Well MVC 1.0.0 gifts you this functionality  within services.AddMvc()

And in case if You want the old PascalCase behavior then you just need to add below lines into your services.AddMvc() of startup.cs class:

services

.AddMvc()

.AddJsonOptions(options =>

options.SerializerSettings.ContractResolver = new DefaultContractResolver());

Please note that it is purely configurable and if you do not wish to use this then just write above line of code in startup.cs.

Happy coding!