0

I asked a preliminary and somewhat similar question here.

Now I need to know how to access the values from within a RESTful URL inside a Controller method/action.

I have a Controller named PlatypusController with a route for it set up like this in WebApiConfig:

config.Routes.MapHttpRoute(
    name: "ReportsApi",
    routeTemplate: "api/{controller}/{unit}/{begindate}/{enddate}",
    defaults: new { enddate = RouteParameter.Optional }
);

PlatypusController.cs has this code:

public void Post()
{
    int i = 2; 
}

The "int i = 2" is nonsense, of course; I just put it there so I could put a breakpoint in the method to verify it was reached. It is when I select "POST" and enter this URL in Postman:

http://localhost:52194/api/Platypus/gramps/201509

(the app is running on port 52194 when I call this)

But to accomplish something worthwhile, I need the "gramps" and the "201509" from the URL. How can I access those? Do I need to add them to the Post method something like this:

public void Post(string unit, string begindate)
{
    DoSomething_Anything(unit, begindate);
}

...or is there some other way to get them, such as from a HttpRequest object or something?

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

3 Answers3

3

I personally prefer to be explicit when defining my routes and that's why I recommend attribute routing instead of convention based routing.

That way, you can explicitly configure routing per controller and action.

Instead of configuring routes this way in the WebApiConfig, just make sure that you have initialized attribute routing by calling this line:

config.MapHttpAttributeRoutes();

in the WebApiConfig file.

Then you can do something like this:

[RoutePrefix("api/platypus")]
public class PlatypusController: ApiController
{
     [Route("{unit}/{begindate}")]
     [HttpPost]
     public void Post(string unit, string begindate)
     {
         int i = 2; 
     }
}

To call this method, do a POST request to: /api/platypus/gramps/201509

HaukurHaf
  • 13,522
  • 5
  • 44
  • 59
  • This works like gangbusters; I do get a "204 No Content" Status in Postman, but I reckon that doesn't matter, as I'm not expeecting any content back anyway. I just wonder why it informs me of that. Isn't it like saying, "It didn't rain in Arizona today" (I wasn't expecting it to). – B. Clay Shannon-B. Crow Raven Dec 29 '15 at 22:57
  • 1
    Glad it works :-). The "204 No Content" is returned by default by the WebAPI "framework" when you have an action which returns nothing (void). According to the spec, it's the way to go. It's basically the same as "200 OK", but it also explicitly tells the client that there is no data. That is, "The request was successful, but I'm not returning any data to you". – HaukurHaf Dec 29 '15 at 23:00
1

Try adding in the Post method [FromBody]

public void Post([FromBody] string unit, [FromBody] string begindate)
{
    DoSomething_Anything(unit, begindate);
}

Check this for detail examples: http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx

Florim Maxhuni
  • 1,421
  • 1
  • 17
  • 35
0

It's as easy as 3.14; this works just dandy:

public void Post(string unit, string begindate)
{
    string _unit = unit;
    string _begindate = begindate;
}

_unit is "gramps" and _begindate is "201509" using this URL:

http://localhost:52194/api/Platypus/gramps/201509

...so it's just what I need.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862