This project is read-only. self-hosted gets statuscode = 405 (method not allowed) for POST but it is OK for GET

Topics: ASP.NET Web API
Dec 27, 2013 at 3:16 AM
The result is the same with Fiddler or using a .NET Http client. I tried several different option without success.

The code is written in F#.

In the following code, test1 (GET) works and returns:
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"]"

But, test2 (POST) fails and returns:
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'."}"

The Client code is:
#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()

//GET http://localhost:8080/api/opc
let r1=client.GetAsync(OPCServerURL).Result
let r1_=r1.Content.ReadAsStringAsync().Result

//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
The Library code is:
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= "*") >]
type OPCController()=
   inherit ApiController()
    // GET /api/opc
    member this.Get() = [| "value1"; "value2" |] |> Array.toSeq

    // POST /api/opc
    member this.Post ([<FromBody>] value:string) = ()

    // PUT /api/opc/5
    member this.Put (id:int) ([<FromBody>] value:string) = ()

    // DELETE /api/opc/5
    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
  let server = new HttpSelfHostServer(config)

  let Open () = 
     printfn "Server ON"
  let Close () =
    server.CloseAsync() |>ignore
    printfn "Server OFF"

The server is started with the script:
#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
I am using the following packages versions:
   "Microsoft.AspNet.Cors" version="5.1.0-rtm-131226" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.1.0-rtm-131226" targetFramework="net45"
  "Microsoft.AspNet.WebApi.Core" version="5.1.0-rtm-131226" targetFramework="net45"
  "Microsoft.AspNet.WebApi.Cors" version="5.1.0-rtm-131226" targetFramework="net45"
  "Microsoft.AspNet.WebApi.SelfHost" version="5.1.0-rtm-131226" targetFramework="net45" />
  "Newtonsoft.Json" version="5.0.8" targetFramework="net45"