1

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

我的应用程序在 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
4

1 回答 1

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

        });
于 2016-08-24T14:15:56.990 回答