如何在 OpenRasta 中显式指定方法绑定?

发布于 2024-09-30 21:34:03 字数 1338 浏览 3 评论 0原文

我在 OpenRasta 的 URL 路由方面遇到了很多困难,尤其是在涉及 PUT 请求时。

假设我有一个假设的 UserContact 处理程序,我需要使用以下签名公开它:

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

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

在这两种情况下,请求正文都包含联系人的表单编码参数。

在我的 Configuration.cs 中,我有:

ResourceSpace.Uses.PipelineContributor<HttpMethodOverriderContributor>();

ResourceSpace.Has.ResourcesOfType<ContactPost>()
    .AtUri("/users/{userId}/contacts/create")
    .And.AtUri("/users/{userId}/contacts/{contactId}")
    .HandledBy<UserContactHandler>()
    .AsXmlDataContract()
    .And.AsJsonDataContract();

我的处理程序有两种方法:

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

我在使用此代码时遇到了两个问题:

  1. 许多客户端(以及我们的 IIS 服务器)不支持 PUT/DELETE。最后,我想通过将上述两个操作都设为 POST 来简化这一点。不幸的是,一旦我这样做,OpenRasta 就无法判断我正在尝试绑定到哪个方法,而且两者都不起作用 - 对于 /create 和 /{contactId} 请求,我都会收到 406 不可接受的消息。

  2. 但是,如果我保留上面的代码,并且客户端通过 X-HTTP-Method-Override: PUT 标头发送,则 /create 请求有效,但 /{contactId} 请求仍返回 406。

它甚至不会进入我的处理程序,所以我不知道从哪里开始调试这样的东西。

有没有办法告诉 OpenRasta 将 URL 路由显式映射到某些方法,例如 ASP.NET MVC 中的方法?或者还有其他明显的我在这里做错的事情吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌生 2024-10-07 21:34:03

您可以使用 HttpOperation 上的 UriName 和 .Named() 扩展方法来帮助 OpenRasta 路由到正确的方法。

对于第二个问题,您应该查看调试输出以了解发生了什么。

一般来说,OpenWrap 会尝试阻止您路由到方法,并尽最大努力让您将不同的资源分开。

在您的示例中,您发布以创建联系人的资源与您的联系人用户是不同的资源,因此可能应该有两个不同的注册。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文