0

Hej,

I am unable to post data to the action method through querystring to the action method which is located in the controller class below is my code.

I type a url "http://localhost:53459/api/esb/post/test" to post value and nothing happens

Any help would be appreciated.

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

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



[RoutePrefix("api/esb")]
public class EsbController : ApiController
{
    [Route("get")]
    [HttpGet]
    public string Get()
    {
        return "Hello there!";
    }

    [Route("post")]
    [HttpPost]
    [AcceptVerbs("POST")]
    public string Post([FromUri]string value)
    {
        return string.Format("{0} is posted successfully ", value);
    }

    [Route("put")]
    [HttpPut]
    public string Put([FromUri] string value)
    {
        return string.Format("{0} is updated successfully ", value);
    }

    [Route("delete")]
    [HttpDelete]
    public string Delete(string value)
    {
        return string.Format("{0} is deleted successfully ", value);
    }
}
Muhammed Shevil KP
  • 1,404
  • 1
  • 16
  • 21
adl
  • 11
  • 1
  • 6
  • You don't need the POST in the url. So it should be like this: `http://localhost:53459/api/esb/test` – alltej Jan 23 '17 at 12:45

3 Answers3

0

If you are typing the url into a browser you are constructing a GET request so it will never reach your Post action. You can confirm this by adding "GET" to the allowed verbs on the action (note: remove the [HttpPost] attribute).

[Route("post")]
[AcceptVerbs("POST", "GET")]
public string Post([FromUri]string value)
{
  return string.Format("{0} is posted successfully ", value);
}
Jon Susiak
  • 4,948
  • 1
  • 30
  • 38
  • Why are using get?. It looks like method is behaving like get – adl Jan 23 '17 at 14:09
  • This is only to demonstrate why your action isn't being hit. As mentioned elsewhere you cannot issue a Post request through the browser unless you use Postman or similar. – Jon Susiak Jan 24 '17 at 15:29
0

remove parameter binding [FromUri] and update the Route like

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

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

Controller

[RoutePrefix("api/esb")]
public class EsbController : ApiController
{
    [Route("get")]
    [HttpGet]
    public string Get()
    {
        return "Hello there!";
    }

    [Route("post/{value}")]
    [HttpPost]
    [AcceptVerbs("POST")]
    public string Post(string value)
    {
        return string.Format("{0} is posted successfully ", value);
    }

}

This is working for me, try to use Postman in chrome or fiddler.

POST http://localhost:XXXXX/api/esb/post/test HTTP/1.1
Ihtsham Minhas
  • 1,415
  • 1
  • 19
  • 31
  • Thanks for reply. I tried this. It does not work. Do you know whats the proble m with it – adl Jan 23 '17 at 14:09
  • If I try this in postman. It is working fine but if i try in browser. It does not show anything – adl Jan 23 '17 at 14:47
  • You cant use browser for post request. If you want to use in website create ajax request. Check this post. http://stackoverflow.com/questions/4797534/how-do-i-manually-fire-http-post-requests-with-firefox-or-chrome – Ihtsham Minhas Jan 23 '17 at 15:25
0

For dot.Net Core (I'm using v2.0), use FromRoute

[AllowAnonymous]
[HttpPost]
[Route("validateEmail/{validationCode}")]
public async Task<IActionResult> ValidateEmail(
 [FromRoute] Guid validationCode)
    {
        await authService.ValidateEmailAsync(validationCode);

        return Ok();
    }

Then post like this:

enter image description here

Assaf S.
  • 4,676
  • 2
  • 22
  • 18