ASP.NET Web API v2 DELETE 405 Errors

The requested resource does not support http method ‘DELETE’

That is the dreaded error that I have been seeing in my VS2013 web api v2 project.  When running the application on IIS express I ran into this issue where I could not use HTTP DELETE against my API controller without getting the 405 error above.  Most of my searches came up with some talk about removing or disabling the WebDAV(which did nothing for me).

What I finally found after many many different configuration combinations was to adjust the allowed http verbs the system.webserver section in the web.config as follows:

<handlers>
 <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
 <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
 <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
 <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
 <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
 <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
 </handlers>

You can also adjust the settings higher in the hierarchy at the applicationhost.config level but I decided for this it would be better to remove the settings and reapply them exactly how I want for this application.

Filtering arrays with jQuery & linqJS

Filtering arrays in JavaScript can be a pain.  In the past I’ve used loops over the array to pull out elements.  It worked but was error prone and a mess usually.After some research I found two different ways that look promising for future use. Below I’m simply filtering an array of customers by active only.

One is a very simple method in the jQuery library, grep:

var activeCustomers = $.grep(customers, function(cust){
     return cust.Active == true;
});

Also I came across a great library for developers familiar with LINQ

http://linqjs.codeplex.com/.  LinqJs allows you to use the a LINQ like syntax to manipulate arrays in a very clean way.  There are two different ways using this library to write the filtering:

var activeLinq1 = Enumerable.From(customers)
    .Where("$.Active==true").ToArray();

var activeLinq2 = Enumerable.From(customers)
     .Where(function (cust) { return cust.Active == true; }).ToArray();

Viewing JSON in Internet Explorer

Recently I have been working on some Web API controllers and found it bothersome to use Internet Explorer to view raw JSON results.  Instead of just seeing the JSON text you get a popup from IE asking how you would like to open up the JSON file.

The “fix” for this is to save the lines below in a text file with the extension “.reg” to modify the registry.

Windows Registry Editor Version 5.00;
; Tell IE 7,8,9,10 to open JSON documents in the browser on Windows XP and later.
; 25336920-03F9-11cf-8FD0-00AA00686F13 is the CLSID for the "Browse in place" .
;
[HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/json]
"CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"
"Encoding"=hex:08,00,00,00

This is telling IE that for the “application/json” MIME type that it can be viewed “in place” or just directly in the browser.  There are a few resources I found with differing information but I found this specific registry fix on Code Project.

MongoDb data access with .NET

Not the usual choice for .NET development but NoSql document databases are becoming more common in the development world.


Getting MongoDb running on Windows

  1. To get started with MongoDb first download the correct version for your operating system.
  2. Create the following new path c:datadb
  3. From the extracted files, run MongoD.exe.
  4. That’s it.
mongodbrunning

.NET with MongoDb

To communicate with MongoDb you need to first download the assembles from github or do it the smart way and use Nuget.  With this C# driver you can now query the database directly using LINQ statements:

There are a few different ways you can construct  the data model when using C# with MongoDb such as using a BsonDocument from a JSON string or do it the “typed” way and use custom objects for your documents.

Note the BsonId Attribute, which is  needed to act as the document identifier.

public class Customer
{
BsonId]
public ObjectId CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime CreatedDate { get; set; }
public IEnumerable Orders { get; set; }
}

Data Access

With MongoDb you do not need to specifically create a new database or collection. When calling the save method, if the database is not there, it will be created, if that collection is not there, it will be created.

Insert

string mongoConn = "mongodb://localhost/?safe=true";
var server = MongoServer.Create(mongoConn);
var myStore = server.GetDatabase("myStore");
var coll = myStore.GetCollection("myCustomer");
var cust = new MongoCustomer()
{
FirstName = "john",
LastName = "doe",
CreatedDate = DateTime.Now,
MongoOrders = GetSomeOrders()
};
coll.Save(cust);

(You can also use the Insert or InsertBatch methods in the MongoCollection Object.

Select

string mongoConn = "mongodb://localhost/?safe=true";
var server = MongoServer.Create(mongoConn);
var myStore = server.GetDatabase("myStore");
var coll = myStore.GetCollection("myCustomer");
var q = from c in coll.AsQueryable()
where c.FirstName.Contains("russ")
select c;

Performance Testing

Our simple data model includes a one to many relationship of a Customers to Orders.

MongoDb vs SQL Server 2008 Express

Insert

mongodb_vs_sqlserver_insert

As you can see, inserting data is faster in MongoDb, especially when the data model gets more complex.

Select

The loaded data for selection testing includes 3,000,000 customers and 10 orders per customer in both sql server and MongoDb.
mongodb_vs_sqlserver_select

With a large amount of data being returned MongoDb is much faster, but it seems the RDBMS had the advantage for smaller queries, at least with this data setup.

If the data structure and business need fall in line with the document database model then using MongoDb with .NET can make some very high performing applications.


Visual Studio 2012 JavaScript Updates

Visual Studio 2012 RC is out and it brings quit a few changes, especially for developing JavaScript.


JavaScript Object Definition

Visual Studio 2012 finally adds in the ability of going to an object’s definition just like when coding in C#.  You can do this by either by right clicking the object or by pressing F12.

vs2012_javascript_object_definition


External JavaScript Intellisense Support

With previous versions of Visual Studio in order to get Intellisense to work with external JavaScript files  you had to add a reference path to a vsdoc file (which usually wouldn’t work without rebuilding Intellisense using ctrl + shift + J):

///&lt;reference path=&quot;jquery-1.6.4-vsdoc.js&quot; /&gt;

In Visual Studio 2012 after the script is added, Intellisense will work with nothing extra needed:

vs2012_jQuery_intellisense


JavaScript Function Documentation

Documentation is something that is usually missing from JavaScript source files. There is now built in support for signatures within JavaScript functions. There can also be multiple signatures for overloading.

Calling Function With Documentation Comments:

    function getTax(rate, amount) {
        ///&lt;signature&gt;
        ///&lt;summary&gt;Get Calculated Tax&lt;/summary&gt;
        ///&lt;param name=&quot;rate&quot; type=&quot;number&quot;&gt;Tax Rate&lt;/param&gt;
        ///&lt;param name=&quot;amount&quot; type=&quot;number&quot;&gt;Money Amount&lt;/param&gt;
        ///&lt;/signature&gt;
        return rate * amount;
    }

vs2012_javascript_withdoc


It seems Visual Studio is now treating Javascript like an actual supported language.