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);
    }
}
1
  • You don't need the POST in the url. So it should be like this: http://localhost:53459/api/esb/test Commented Jan 23, 2017 at 12:45

3 Answers 3

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Why are using get?. It looks like method is behaving like get
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.
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

3 Comments

Thanks for reply. I tried this. It does not work. Do you know whats the proble m with it
If I try this in postman. It is working fine but if i try in browser. It does not show anything
You cant use browser for post request. If you want to use in website create ajax request. Check this post. stackoverflow.com/questions/4797534/…
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.