如何在Mono上的xsp4中启用运行Nancy的PUT和DELETE HTTP方法?

时间:2014-07-27 23:01:58

标签: c# mono nancy xsp

我必须错过一些非常容易的事情。我正在使用Xamarin Studio编写NancyFx应用程序。我想在我的模块中使用PUT和DELETE HTTP方法,但是当我发出PUT或DELETE请求时,我回到405 Method Not Allowed并在HTTP响应头中看到Allow: GET, POST

我的应用程序在OSX上的Xamarin Studio中的xsp4下运行。我在此问题上遇到的大多数解决方案仅在IIS下运行时才有意义。

如何在xsp4 / Mono / Nancy中启用PUT和DELETE?我不相信南希是个问题。我很确定这仅限于Mono上的xsp4服务器。我在web.config文件中遗漏了什么(在下面发布)?

我的Nancy模块中的DELETE处理程序如下所示:

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);
};

我的web.config文件非常简单。它几乎只能在所有路径和动词上启用Nancy,如下所示:

<?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>

以下是来自curl的HTTP会话:

> 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

1 个答案:

答案 0 :(得分:0)

有点晚了,但这可能是一个问题,需要在Nancy bootstrapper中启用CORS上的DELETE方法:

    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");

        });