0

I'm still looking for help regarding below issue:

I've been working on a RESTful API in a MVC C#.NET (4.0) Web API project for my companies over a few days now (on and off) but for some reason I can't seem to get the API to route to the HTTP POST, but keeps on defaulting to Get (I'm assuming..).

I've got a few GET requests working fine, I've even stress-tested them, this leads me to believe there is a routing error somewhere that I can't seem to find...

I get this error message when navigating to (I've tried many other parameters too, but the debugger won't catch the Post method firing).

{"Message":"The requested resource does not support http method 'GET'."} localhost:61090/api/Ole_foundations/Post/?s=kalle

This is my Post:

[HttpPost]
public void Post(string s)
{
    WSC.DNN.WorksiteCloudOLEDC.WorkSiteCloudOLEDC.WorkSiteCloudOLEDC wdc = new WSC.DNN.WorksiteCloudOLEDC.WorkSiteCloudOLEDC.WorkSiteCloudOLEDC();
    int ret1 = 0;
    int ret2 = 0;
    Boolean ret3 = wdc.INSERTOLE_Foundation(14774, 14774, "JSONTEST", "JSONTEST", "JSONTEST", 0.00, 0.00, 0.00, 0.000, 0.00, 0.000, "JSONTEST", 0.000, 0.000, 12345, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, "JSONTEST", 1234, 0.000, 0.000, 0.000, 0.000, true, true, Convert.ToDateTime("01/01/1900"), "JSONTEST", "JSONTEST", true, "JSONCOMMENTTEST", 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, true, true, true, true, true, true, true, 12345, true, true, 12345, true, true, "JSONTEST", "JSONTEST", "JSONTEST", null, Convert.ToDateTime("01/01/1900"), "JSONTEST", null, Convert.ToDateTime("01/01/1900"), Convert.ToDateTime("01/01/1900"), Convert.ToDateTime("01/01/1900"), null, null, null, true, true, true, "JSONTEST", 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, null, "FINALJSONTEST", true, "NRCJSONTEST", 12345, true, true, true, true, true, true, "UPDATEDBYTEST", 3322, WSC.DNN.GeneralRoutines.clsEnums.MappingTypes.OLE_Foundation, conStr, "JoakimJSONTEST", ref ret1, ref ret2);


}

Everything in the method-body is fine and works as intended.

These are the routes as defined in WebApiConfig.cs:

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{parameters}",
            defaults: new { parameters = RouteParameter.Optional }
        );

        config.Routes.MapHttpRoute(
            name: "ApiByAction",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { controller = "Ole_foundations", action = "GetFromThreads", id = RouteParameter.Optional }
        );

        config.Routes.MapHttpRoute(
            name: "insertRoute",
            routeTemplate: "api/{controller}/{action}/{param}",
            defaults: new { controller = "Ole_foundations", action = "Post", param = RouteParameter.Optional }
        );

And these are the routes defined in **Global.asax.cs:**


    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles); 
            RouteTable.Routes.MapHttpRoute(null, "api/{controller}/{action}/{parameters}",
             new { parameters = UrlParameter.Optional, Action = "Get" });
            RouteTable.Routes.MapHttpRoute(null, "api/{controller}/{action}/{param}",
             new { Action = "Post", param = UrlParameter.Optional });
        }

        } 
geostocker
  • 1,190
  • 2
  • 17
  • 29

2 Answers2

1

You are making a GET and your method is marked as [HttpPost] alsto the argument that you pass should be the same name as the parameter in this case s=someValue

Jeradotz
  • 66
  • 7
  • Well, clearly it's recognized as a GET, that's why I'm asking for help... Doesn't matter if I change the parameter either, still get the same error message... – geostocker Sep 26 '16 at 08:29
  • An HTTP POST request cannot be sent by using just an URL. (Except for the special case that there are no form fields and that the server doesn't care about the Content-type header.) . You can use a
    , javascript or jQuery to make the POST, just google those options, or by calling them inside C# http://stackoverflow.com/questions/4015324/http-request-with-post
    – Jeradotz Sep 26 '16 at 16:15
0

The message you get means that you are accessing the correct path but that you are using a GET request instead of POST which is why it doesn't work.

This should work though:

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 's=kalle' "http://localhost:61090/api/Ole_foundations/Post/"
CodingNagger
  • 1,178
  • 1
  • 11
  • 26