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

  1. @using System.Text.RegularExpressions  
  2. <script src="~/Scripts/jquery-1.8.2.js"></script>  
  3. <script src="~/Scripts/knockoutjs%202.1.0.js"></script>  
  4. <script src="~/Scripts/jquery-ui-1.10.3.js"></script>  
  5. <link href="~/Content/themes/base/jquery.ui.all.css" rel="stylesheet" />  
  6. <link href="~/Areas/HelpPage/TestClient.css" rel="stylesheet" />  
  7. @* Automatically grabs the installed version of JQuery/JQueryUI/KnockoutJS *@  
  8. @{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)";}  
  9. @foreach (var item in Directory.GetFiles(Server.MapPath(@"~/Scripts")).Where(f => Regex.IsMatch(f, filePattern)))  
  10. {  
  11.     <script src="~/Scripts/@Path.GetFileName(item)"></script>  
  12. }  
  13. <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:

  1. @using System.Web.Http  
  2. @using cors.Areas.HelpPage.Models  
  3. @model HelpPageApiModel  
  4. @{  
  5.     var description = Model.ApiDescription;  
  6.     ViewBag.Title = description.HttpMethod.Method + " " + description.RelativePath;  
  7. }  
  8. <div id="body">  
  9.     <section class="featured">  
  10.         <div class="content-wrapper">  
  11.             <p>  
  12.                 @Html.ActionLink("Help Page Home""Index")  
  13.             </p>  
  14.         </div>  
  15.     </section>  
  16.     <section class="content-wrapper main-content clear-fix">  
  17.         @Html.DisplayFor(m => Model)  
  18.     </section>  
  19. </div>  
  20. @Html.DisplayForModel("TestClientDialogs")  
  21. @section Scripts {  
  22.     <link type="text/css" href="~/Areas/HelpPage/HelpPage.css" rel="stylesheet" />  
  23.      @Html.DisplayForModel("TestClientReferences")  
  24. }   

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

  • In this wnidow type the name "Nightly Builds" and Source "http://www.myget.org/F/aspnetwebstacknightly/".

    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


Similar Articles