Pages

Monday, January 25, 2016

This is a quick post on how to enable CORS in Web API 2. It’s actually fairly an easy task. First you need to install Microsoft.AspNet.WebApi.Cors nuget package to the Web API project. You can do it from either of following ways.

After the installation, create a folder and open up a command prompt there (shift+right click on the folder and you can select Open command window here)

Open command window here

On the command prompt, just type dotnet and check the various options.

dotnet

According to the options, let’s start by running dotnet new command.

dotnet new

You can see some new files got created in the folder.

Project Folder

Here the Nuget.Config file contains the nuget v3 endpoints for grabbing .NET core libraries. The Program.cs is a simple application which will print “Hello World” to the console. The project.json will contain project’s dependencies, project’s target frameworks etc.

Next step is building the project. For that we can run dotnet restore command.

dotnet restore

When you run dotnet restore for the first time, you will see that a lot of libraries getting downloaded from nuget. Since I have already ran dotnet restore before for other projects, it has already downloaded the libraries into .dnx folder located in C:\Users\[user].

Once the package restoration completes, you can run dotnet run.

dotnet run

You don’t need to compile, the run command would do a compile and execute. Now if you examine your project folder, you will see that bin folder is created and there is a .dotnetrun folder and it has no assemblies there.

.dotnetrun Folder

If you want to get the assemblies, you can run dotnet compile command.

dotnet compile

And now if you examine the folder, you will see a folders created “bin\Debug\dnxcore50”, and you can find the executables there.

Monday, January 4, 2016

JSHint is one of the most popular JavaScript Code Quality Tools which is widely been used among the JavaScript developers. In this post let’s see how we can use JSHint with an ASP.NET 5 application using Visual Studio 2015.

First let’s create an ASP.NET 5 Web Application.

ASP.NET 5 Web Application.

Then from the Solution Explorer, under the project, expand “Dependencies” node. Right click on “npm” folder and open up the package.json file.

Open package.json

Add the “gulp-jshint” devDependency as follows (last line).

{

"name": "ASP.NET",

"version": "0.0.0",

"devDependencies": {

"gulp": "3.8.11",

"gulp-concat": "2.5.2",

"gulp-cssmin": "0.1.7",

"gulp-uglify": "1.2.0",

"rimraf": "2.2.8",

"gulp-jshint": "2.0.0"

}

}

Upon saving of the file, you will notice that relevant packages are getting downloaded/restored.

Next step is to modify the gulpfile.js and add a new task to run JSHint.

For that Open up the gulpfile.js and include the “gulp-jshint” plugin as follows (last line).

Now that is it. Open up the Task Runner Explorer and you should be able to see the new jshint task you have just created.

Task Runner Explorer

For testing the task, first let’s add some JavaScript codes to js files where the jshint task is watching. We have mentioned that jshint task is watching .js files inside wwwroot/js folder. In the default ASP.NET 5 project template there is a file named site.js, we can easily modify that.

I am adding following code there and you might notice that there are couple of issues with the code.

// Write your Javascript code.

"use strict";

function sayHello() {

return'Hello World!';

}

sayHello()

sayHelloWorld();

To find out what are those, from the Task Runner Explorer let’s run the jshint task.

Task Runner Explorer

Task Runner Explorer

As you can see jshint is showing up the list of issues. Even we can bind the task to run once the project is built.

Task Runner Explorer

So when the project is built, you will see the list of issues (if any) in the Task Runner Explorer. So now you can start fixing those.

Saturday, January 2, 2016

Received the precious Microsoft Most Valuable Professional (MVP) Award for the third consecutive year. And this time in Visual Studio and Development Technologies. In the last two years, I have been awarded MVP for Visual C# and then .NET.

Looking forward for a year filled with action on top of Microsoft Development Stack.