自我托管的Asp.net-web-api获取statuscode = 405 (method not allowed)
POST
但OK
为GET
。
结果与Fiddler或使用.NET Http客户端相同。
我尝试了几种不同的选择但没有成功。
代码用F#编写。
在以下代码中,test1(GET)工作并返回:
val r1 : HttpResponseMessage =
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Date: Thu, 26 Dec 2013 18:18:24 GMT
Server: Microsoft-HTTPAPI/2.0
Content-Length: 19
Content-Type: application/json; charset=utf-8
}
val r1_ : string = "["value1","value2"]"
但是,test2失败并返回:
val r2 : HttpResponseMessage =
StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Date: Thu, 26 Dec 2013 18:20:35 GMT
Server: Microsoft-HTTPAPI/2.0
Content-Length: 73
Allow: GET
Content-Type: application/json; charset=utf-8
}
val r2_ : string =
"{"Message":"The requested resource does not support http method 'POST'."}"
客户端代码是:
#r "System.Net.Http"
open System
open System.Text
open System.Net.Http
let OPCServerURL=new Uri("http://localhost:8080/api/opc")
let client= new HttpClient()
//test1
//GET http://localhost:8080/api/opc
let r1=client.GetAsync(OPCServerURL).Result
let r1_=r1.Content.ReadAsStringAsync().Result
//test2
//POST http://localhost:8080/api/opc
let content1 = new System.Net.Http.StringContent("testString", Encoding.UTF8, "application/json");
let r2=client.PostAsync(OPCServerURL,content1).Result
let r2_=r2.Content.ReadAsStringAsync().Result
图书馆代码是:
namespace OPC_REST_1
open System
open System.Web
open System.Net
open System.Net.Http
open System.Web.Http
open System.Web.Http.SelfHost
open System.Web.Http.Cors
[< EnableCors(origins="*", headers="*",methods= "*") >]
[<RoutePrefix("api/opc")>]
type OPCController()=
inherit ApiController()
// GET /api/opc
[<HttpGet>]
member this.Get() = [| "value1"; "value2" |] |> Array.toSeq
// POST /api/opc
[<HttpPost>]
member this.Post ([<FromBody>] value:string) = ()
// PUT /api/opc/5
[<HttpPut>]
member this.Put (id:int) ([<FromBody>] value:string) = ()
// DELETE /api/opc/5
[<HttpDelete>]
member this.Delete (id:int) = ()
module ServiceHost =
type ApiRoute = { controller: string;action:string;id : RouteParameter }
let config = new HttpSelfHostConfiguration( Uri "http://localhost:8080/" )
config.Routes.MapHttpRoute(name="DefaultApi", routeTemplate="api/{controller}/{id}",
defaults={controller="OPC";action="Get";id = RouteParameter.Optional}) |> ignore
config.EnableCors()
config.MapHttpAttributeRoutes()
let server = new HttpSelfHostServer(config)
let Open () =
printfn "Server ON"
server.OpenAsync().Wait()
let Close () =
server.CloseAsync() |>ignore
printfn "Server OFF"
使用脚本启动服务器:
#r @"C:\Users\Celso\Documents\Visual Studio 2013\Projects\OPC_REST_1\OPC_REST_1\bin\Debug\OPC_REST_1.dll"
open OPC_REST_1
ServiceHost.Open()
我正在使用以下软件包版本:
"Microsoft.AspNet.Cors" version="5.1.0-rc1" targetFramework="net45"
"Microsoft.AspNet.WebApi.Client" version="5.1.0-rc1" targetFramework="net45"
"Microsoft.AspNet.WebApi.Core" version="5.1.0-rc1" targetFramework="net45"
"Microsoft.AspNet.WebApi.Cors" version="5.1.0-rc1" targetFramework="net45"
"Microsoft.AspNet.WebApi.SelfHost" version="5.1.0-rc1" targetFramework="net45"
"Newtonsoft.Json" version="4.5.11" targetFramework="net45"