.NET的Web API - 几个GET和POST方法 [英] .NET Web Api - several GET and POST methods

查看:257
本文介绍了.NET的Web API - 几个GET和POST方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个REST API并具有以下问题:

I'm writing a REST API and have the following problem:

随着资源/消息/,我在我的MessageController以下方法:

With the resource /messages/, I have the following methods in my MessageController:

[HttpGet]
// retrieves all messages for a user
public HttpResponseMessage GetMessages(Guid RecordId) {}

[HttpGet]
// retrieves only a single messages, specified by guid
public HttpResponseMessage GetMessage(Guid RecordId) {}

在我WebApiConfig.cs我有以下路径:

In my WebApiConfig.cs I have the following routing:

config.Routes.MapHttpRoute(
    name: "MessageRouting",
    routeTemplate: "messages/{RecordId}/",
    defaults: new { controller = "Message", RecordId= RouteParameter.Optional }
);

这,当然,失败的原因是布线不知道该调用哪个方法。 这里的问题是,保持平安,我想避免额外的查询参数,如

This, of course, fails because the routing doesn't know which method to call. The issue here is, to remain RESTful, I want to avoid having additional query parameters such as

GET /messages/?userid=1

如果我宁愿从AccessToken拉用户标识和调用的getMessages如下?

Should I rather pull the userId from the AccessToken and call GetMessages as follows?

GET /messages
AccessToken=foo

我会遇到同样的问题,用[HttpPost]和[HttpPut],以及 - 我总是有一张收藏,或单个项目,我想使用的,所以对于每个HTTP方法我将至少有2方法调用。

I will run into the same problem using [HttpPost] and [HttpPut] as well - I always have either a collection, or a single item that I want to work with, so for each Http Method I will have at least 2 Methods to call.

我没有移动的getMessages()方法将UserController中的选项,因为在那里我会遇到同样的问题 - 似乎与网络的API,控制器只能有每个HTTP方法定义一个单一的时间,这使得它可怕很难与收藏与单品的工作。

I don't have the option of moving a GetMessages() method to the UserController, because there I will run into the same problem - it seems that with Web Api, a controller can only have each HTTP Method defined a single time, which makes it horribly hard to work with collections vs. single items.

我有点剩余REST风格,并具有网址之间徘徊的唯一确定的资源,但似乎.NET没有给我选择在这里,因为我不能指定的HTTP方法路由。或者,可以吗?

I'm a little torn between remaining RESTful and having URLs uniquely identify resources, but it seems .NET doesn't give me a choice here, as I can't specify the HTTP Method in the routing. Or can I?

推荐答案

找到了解决办法 - 在WebApiConfig.cs,您可以指定name属性的路线。该属性可用于注释的方法,如:

Found the solution - in WebApiConfig.cs, you can specify the "name" attribute for routes. This attribute can be used to annotate the methods, such as:

public class MessageController() {

    [ActionName("MultipleMessages")]
    [HttpGet]
    public HttpResponseMessage GetMessages(Guid UserId) {}

    [ActionName("SingleMessage")]
    [HttpGet]
    public HttpResponseMessage GetMessage(Guid MessageId) {}

}

这样,你可以在同一个控制器几个GET或POST方法。路由调用这些方法是这样的:

That way you can have several GET or POST methods in the same controller. The route calling these methods would look like this:

// multiple messages, will call MessageController.GetMessages()
config.Routes.MapHttpRoute(
    name: "MultipleMessages",
    routeTemplate: "users/{UserId}/messages",
    defaults: new { controller = "Message", UserId = RouteParameter.Optional }
);

// single messages, will call MessageController.GetMessage()
config.Routes.MapHttpRoute(
    name: "SingleMessage",
    routeTemplate: "messages/{MessageId}/",
    defaults: new { controller = "Message", MessageId = RouteParameter.Optional }
);

这篇关于.NET的Web API - 几个GET和POST方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆