ASP.Net Web API CORS Supporting Using the Nightly Builds

Introduction

In this article I will explain how to use the Package "Microsoft.AspNet.Cors" from the nightly builds.

Nightly Build is a framework. We strongly advize that it should only used for official builds for production. It is used by the developers for fixing the bugs and new features of an official prerelease. There is no guarantee that the nightly build is available or will work. Now I define all the steps of creating the project.

Step 1

First we create the Web API project as in the following:

  • Start Visual Studio 2012 and select "New Project" from the Start Page.
  • In the Template window, select "Installed template" -> "Visual C#" -> "Web".
  • Choose "ASP. NET MVC 4 Web Application" then change its name.
  • Click on the "OK" button.

up5.jpg

  • MVC 4 Project window:

up6.jpg

  • Select "Web API" and click on the "OK" button.

Step 2

Now we install the client package.

  • Go to the "Tools".
  • Choose "Library Package Manager" -> "Manage Nuget Packages" for the solution.
  • Make sure to "include Prerelease".
  • Type in the search box "webapitestclient".

cors.jpg

  • Now click to install.

When the Web API Test Client is installed, the following files will be added to your application:

  • Scripts\WebApiTestClient.js
  • Areas\HelpPage\TestClient.css
  • Areas\HelpPage\views\Help\Display Template\TestClientReferences.cshtml
  • Areas\HelpPage\views\Help\Display Template\TestClientDialogs.cshtml

Step 3

Now open the "TestClientReference.cshtml" file. This file exists in the Solution Explorer: "Areas\HelpPage\Views\DisplayTemplates\TestClientReferences.cshtml".

And write this code.

@using System.Text.RegularExpressions

 

<script src="~/Scripts/jquery-1.8.2.js"></script>

 

<script src="~/Scripts/knockoutjs%202.1.0.js"></script>

 

<script src="~/Scripts/jquery-ui-1.10.3.js"></script>

 

<link href="~/Content/themes/base/jquery.ui.all.css" rel="stylesheet" />

 

<link href="~/Areas/HelpPage/TestClient.css" rel="stylesheet" />

 

@* Automatically grabs the installed version of JQuery/JQueryUI/KnockoutJS *@

 

@{var filePattern = @"(jquery-[0-9]+.[0-9]+.[0-9]+.js|jquery-ui-[0-9]+.[0-9]+.[0-9]+.js|knockout-[0-9]+.[0-9]+.[0-9]+.js)";}

 

@foreach (var item in Directory.GetFiles(Server.MapPath(@"~/Scripts")).Where(f => Regex.IsMatch(f, filePattern)))

 

{

 

    <script src="~/Scripts/@Path.GetFileName(item)"></script>

 

}

 

<script src="~/Scripts/WebApiTestClient.js" defer="defer"></script>

In this code we use some JavaScripts files that exist in the "Script" folder. We can drag and drop these files from the Script folder. These files are:

  • jquery-1.8.2.js
  • knochoutjs2.1.0.js
  • jquery.ui.1.10.3.js

Step 4

Now open the "Api.cshtml" file. This file exists in Solution Explorer: "Areas\HelpPage\Views\Help\Display". And write this code:

@using System.Web.Http

@using cors.Areas.HelpPage.Models

@model HelpPageApiModel

 

@{

    var description = Model.ApiDescription;

    ViewBag.Title = description.HttpMethod.Method + " " + description.RelativePath;

}

 

<div id="body">

    <section class="featured">

        <div class="content-wrapper">

            <p>

                @Html.ActionLink("Help Page Home", "Index")

            </p>

        </div>

    </section>

    <section class="content-wrapper main-content clear-fix">

        @Html.DisplayFor(m => Model)

    </section>

</div>

@Html.DisplayForModel("TestClientDialogs")

@section Scripts {

    <link type="text/css" href="~/Areas/HelpPage/HelpPage.css" rel="stylesheet" />

     @Html.DisplayForModel("TestClientReferences")

 

}

 

Step 5

Now we add the "Nightly Build" to this project.

  • Go to Tools menu.
  • "Library Package Manager"->"Package Manager Setting".
  • Open Window like this.

up8.jpg

cors7.jpg

  • Click on "Update" and then "Ok".

Step 6

Use the Nightly Builds as in the following:

  • In the "Solution Explorer" right-click on the Project and select "Manage Nuget Packages".
  • Now from the Window expand the "Online" and then select the "Nightly Builds".
  • Make sure that "Include Prerelease".
  • Select the "Entity Framework".

s1.jpg

  • And install it.

Step 7

Now install the MicroSoft.AspNet.WebApi.Cors from the nightly builds.

corsupdate.jpg

Step 8

Execute the application.

up11.jpg

Click on "API".

Then click on "GET/api/values ".

up2.jpg


Click on "Test API".

up3.jpg

Now click on the "Send" button. The output looks like this:

up4.jpg