1

I must be missing something incredibly easy. I'm writing a NancyFx app using Xamarin Studio. I want to use the PUT and DELETE HTTP methods in my module, but when I issue a PUT or DELETE request, I get back 405 Method Not Allowed and I see Allow: GET, POST in the HTTP response header.

My app is running under xsp4 in Xamarin Studio on OSX. Most of the solutions I've seen to this problem are only relevant when running under IIS.

How do I enable PUT and DELETE in xsp4/Mono/Nancy? I don't believe Nancy is the problem. I'm pretty sure this is confined to the xsp4 server on Mono. Am I missing something in my web.config file (posted below)?

The DELETE handler in my Nancy module looks like this:

Delete["/api/family/{id}"] = _ => 
{
    int rows = 0;

    using (IDbConnection con = dbFactory.Open())
        rows = con.Execute("DELETE FROM Family WHERE Id=?", new { Id = _.id });

    return Response.AsJson(rows, HttpStatusCode.OK);
};

My web.config file is very simple. It pretty much just enables Nancy on all paths and verbs, like so:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <httpHandlers>
      <add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
    </httpHandlers>
    <compilation>
      <assemblies>
        <add assembly="Mono.Data.Sqlite, Version=4.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756" />
        <add assembly="System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        <add assembly="System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <add assembly="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      </assemblies>
    </compilation>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <httpErrors existingResponse="PassThrough" />
    <handlers>
      <add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
    </handlers>
  </system.webServer>
</configuration>

Here is the HTTP conversation from curl:

> PUT /api/family HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:8080
> Accept: */*
> Content-Type: application/json
> Content-Length: 262
> 
* upload completely sent off: 262 out of 262 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 405 Method Not Allowed
< Date: Sun, 27 Jul 2014 21:37:46 GMT
< Server: Mono.WebServer2/0.4.0.0 Unix
< Allow: GET, POST
< X-AspNet-Version: 4.0.30319
< Content-Length: 0
< Cache-Control: private
< Content-Type: text/html
< Keep-Alive: timeout=15, max=100
* HTTP/1.0 connection set to keep alive!
< Connection: Keep-Alive
< 
* Connection #0 to host localhost left intact
Andy S
  • 8,641
  • 6
  • 36
  • 40
  • This is most likely an issue with the Web Server you're using. Are you using Apache or nginx? – Phill Jul 28 '14 at 11:08
  • My app is running under xsp4, the dev HTTP/ASP.NET server that is bundled with Mono/Xamarin Studio. Right now, I suspect that xsp4 simply doesn't support PUT/DELETE and I would need to hook up mod_mono to Apache or nginx to get it to work. – Andy S Jul 28 '14 at 22:18

1 Answers1

0

A little bit late, but it can be a problem related with the need to enable the DELETE method on CORS in Nancy bootstrapper:

    protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
    {
        base.RequestStartup(container, pipelines, context);

        //CORS Enable
        pipelines.AfterRequest.AddItemToEndOfPipeline((ctx) =>
        {
            ctx.Response.WithHeader("Access-Control-Allow-Origin", "*")
                            .WithHeader("Access-Control-Allow-Methods", "POST,GET,DELETE")
                            .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type");

        });
Gerard Carbó
  • 1,775
  • 18
  • 16