ploeh / Hyprlinkr

A URI building helper library for ASP.NET Web API
MIT License
197 stars 34 forks source link

Example to create a link to a POST methods? #28

Closed pmcevoy closed 10 years ago

pmcevoy commented 10 years ago

Hi Mark, I would like to create a link to a method that receives POSTs (later I will be using the other verbs too).

I have an Action Method on my RoutingRuleController:

[HttpPost]
public ResourceResponse<RoutingRule> Add([FromBody] RoutingRule routingRule)
{
...
}

I then have the following Route declared:

            _routes.MapHttpRoute(
                name: "CreateItem",
                routeTemplate: "api/{controller}",
                defaults: new
                    {
                        action = "post"
                    },
                constraints: new
                    {
                        httpMethod = new HttpMethodConstraint(HttpMethod.Post)
                    }
                );

(ResourceResponse is our implementation of HAL compatible objects)

When I use hyprlinkr to generate a link to this method:

var href = _linker.GetUri<RoutingRuleController>(c => c.Add(new RoutingRule()));

it generates:

http://dogsmanager:9600/api/routingrule?routingRule=Dogs.Contract.Models.RoutingRule

when really it should be:

http://dogsmanager:9600/api/routingrule

(the documented naming convention in my HAL link relations indicate that this should be a POST of the RoutingRule resource, so that is how we communicate to the client the correct method to use).

In this case, any param that is decorated with [FromBody] should not be included in the generated Uri

Is the library only intended for linking to GET methods?

BTW, it also feels weird doing:

var href = _linker.GetUri<RoutingRuleController>(c => c.Add(new RoutingRule()));

But if I do:

var href = _linker.GetUri<RoutingRuleController>(c => c.Add(null));

I get NullRefException. Is HyprLinkr invoking the controller method?

ploeh commented 10 years ago

I normally address that scenario with a custom route dispatcher.

ploeh commented 10 years ago

Does this answer your question?

ricmrodrigues commented 10 years ago

Hi Mark. Why wouldn't POST Uri generation be included in hyprlinkr instead of we having to resort to a custom route dispatcher? Thanks

pmcevoy commented 10 years ago

Sorry for lack of feedback: I swapped to the SO posting and updated there. You can close this issue

ploeh commented 10 years ago

@ricmrodrigues How would it work? It would need to reliably filter away any 'model' parameter representing data being POSTed, but still include other parameters, including, perceivably, query parameters.

Perhaps the ASP.NET Web API has a way to figure that out, but if so, I don't know how it works... I wouldn't mind adding the feature if it's possible, though...

ricmrodrigues commented 10 years ago

Can't we rely on the [FromBody] for that?

ploeh commented 10 years ago

Rely on? I don't know... I never use [FromBody]... In fact, this is the first time I hear about this attribute.

ricmrodrigues commented 10 years ago

Well, in that case, it wouldn't work. But you just rely on Web API figuring out what comes from where? I guess without [FromBody] it wouldn't be possible. Could be a pre-req, is you don't use [FromBody] you can't generate POST URIs.

ploeh commented 10 years ago

Web API usually figures it out for me without problems, so I haven't looked into the details of how it does it.

Here's an example method signature:

public HttpResponseMessage Post(string id, ProductRendition productRendition)