【发布时间】:2016-08-24 22:15:56
【问题描述】:
我一定错过了一些非常简单的东西。我正在使用 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
【问题讨论】:
-
这很可能是您使用的 Web 服务器的问题。你用的是 Apache 还是 nginx?
-
我的应用程序在 xsp4 下运行,这是与 Mono/Xamarin Studio 捆绑的开发 HTTP/ASP.NET 服务器。现在,我怀疑 xsp4 根本不支持 PUT/DELETE,我需要将 mod_mono 连接到 Apache 或 nginx 才能使其工作。