开发者

How to Explicitly Specify Method Bindings in OpenRasta?

开发者 https://www.devze.com 2023-01-23 20:16 出处:网络
I\'m having a lot of difficulty with OpenRasta\'s URL routing, especially when it comes to PUT requests.

I'm having a lot of difficulty with OpenRasta's URL routing, especially when it comes to PUT requests.

Let's say I have a hypothetical UserContact handler, and I need to expose it with the following signature:

//Create a new contact:
POST /users/{userId}/contacts/create

//Update a contact:
PUT /users/{userId}/contact/{contactId}     

In both cases, the request body contains form-encoded parameters for the contact.

In my Configuration.cs I have:

ResourceSpace.Uses.PipelineContributor<HttpMethodOverriderContributor>();

ResourceSpace.Has.ResourcesOfType<ContactPost>()
    .AtUri("/users/{userId}/contacts/create")
开发者_运维百科    .And.AtUri("/users/{userId}/contacts/{contactId}")
    .HandledBy<UserContactHandler>()
    .AsXmlDataContract()
    .And.AsJsonDataContract();

My Handler has two methods:

public OperationResult Post(int userId, ContactPost contact)
public OperationResult Put(int userId, int contactId, ContactPost contact)

I run into two problems with this code:

  1. Many clients (and also our IIS server) don't support PUT/DELETE. Ultimately I'd like to just simplify this by making both of the above operations a POST. Unfortunately as soon as I do that, OpenRasta is unable to tell which method I'm trying to bind to and neither one works - I'll get a 406 Not Acceptable for both the /create and the /{contactId} request.

  2. If however I keep the code as above and the client sends through the X-HTTP-Method-Override: PUT header then the /create request works, but the /{contactId} request still returns a 406.

It doesn't even step into my handler, so I'm not sure where to start debugging something like this.

Is there any way to tell OpenRasta to explicitly map URL routes to certain methods like in ASP.NET MVC? Or is there something else obvious I'm doing wrong here?


You can use the UriName on HttpOperation and the .Named() extension metod to help OpenRasta route to the correct method.

For your second problem, you should look at the debug output to see what is happening.

In genera, OpenWrap tries to stop you from routing to methods, and tries as hard as it can to get you to separate resources for resources that are different.

In your example, the resource to which you post to create a contact is a different resource from your contact user, and as such should probably have two different registrations.

0

精彩评论

暂无评论...
验证码 换一张
取 消