回答(10)
2 years ago
这个记录标记是什么意思?如果这仅用于记录目的,我将使用GET并禁用所有缓存,因为您要记录此资源的每个查询 . 如果记录标记有另一个目的,POST就是要走的路 . 用户应该知道,他的动作影响系统,POST方法是一个警告 .
2 years ago
我认为最简单的方法就是使用 AttributeRouting .
在您的控制器中显而易见,您为什么要在Global WebApiConfig 文件中使用它?
例:
[Route("api/YOURCONTROLLER/{paramOne}/{paramTwo}")]
public string Get(int paramOne, int paramTwo)
{
return "The [Route] with multiple params worked";
}
{} 名称需要与您的参数匹配 .
就这么简单,现在你有一个单独的 GET 来处理这个实例中的多个参数 .
2 years ago
只需为 WebApiConfig 条目添加新路由即可 .
例如,致电:
public IEnumerable Get(int pageNumber, int pageSize) { ..
加:
config.Routes.MapHttpRoute(
name: "GetPagedData",
routeTemplate: "api/{controller}/{pageNumber}/{pageSize}"
);
然后将参数添加到HTTP调用:
GET ///Api/Data/2/10
2 years ago
我只需要实现一个RESTfull api,我需要传递参数 . 我通过传递查询字符串中的参数来完成此操作,其格式与Mark的第一个示例“api / controller?start = date1&end = date2”所描述的相同 .
// uri: /api/courses
public IEnumerable Get()
{
NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query);
var system = nvc["System"];
// BL comes here
return _courses;
}
在我的情况下,我通过Ajax调用WebApi看起来像:
$.ajax({
url: '/api/DbMetaData',
type: 'GET',
data: { system : 'My System',
searchString: '123' },
dataType: 'json',
success: function (data) {
$.each(data, function (index, v) {
alert(index + ': ' + v.name);
});
},
statusCode: {
404: function () {
alert('Failed');
}
}
});
我希望这有帮助...
2 years ago
public class ResourceQuery
{
public string Param1 { get; set; }
public int OptionalParam2 { get; set; }
}
public class SampleResourceController : ApiController
{
public SampleResourceModel Get([FromUri] ResourceQuery query)
{
// action
}
}
2 years ago
@LukLed清楚地解释了使用GET或POST . 关于你可以传递参数的方法,我建议采用第二种方法(我对ODATA也不太了解) .
1.将params转换为单个JSON字符串并在API中将其拆分 . http://forums.asp.net/t/1807316.aspx/1
这不是用户友好和SEO友好
2.在查询字符串中传递参数 . 将多个查询参数传递给restful api的最佳方法是什么?
这是通常的优选方法 .
3.定义路径中的参数:api / controller / date1 / date2
这绝对不是一个好方法 . 这让人觉得有一个 date2 是 date1 的子资源,事实并非如此 . date1 和 date2 都是查询参数,并且处于同一级别 .
在简单的情况下,我会建议像这样的URI,
api/controller?start=date1&end=date2
但我个人喜欢下面的URI模式,但在这种情况下,我们必须编写一些自定义代码来映射参数 .
api/controller/date1,date2
2 years ago
[Route("api/controller/{one}/{two}")]
public string Get(int One, int Two)
{
return "both params of the root link({one},{two}) and Get function parameters (one, two) should be same ";
}
根链接(,)和Get函数参数(一,二)的两个参数应该相同
2 years ago
public HttpResponseMessage Get(int id,string numb)
{
//this will differ according to your entity name
using (MarketEntities entities = new MarketEntities())
{
var ent= entities.Api_For_Test.FirstOrDefault(e => e.ID == id && e.IDNO.ToString()== numb);
if (ent != null)
{
return Request.CreateResponse(HttpStatusCode.OK, ent);
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Applicant with ID " + id.ToString() + " not found in the system");
}
}
}
2 years ago
我知道这真的很老了,但我最近想要同样的东西,这就是我发现的......
public HttpResponseMessage Get([FromUri] string var, [FromUri] string test) {
var retStr = new HttpResponseMessage(HttpStatusCode.OK);
if (var.ToLower() == "getnew" && test.ToLower() == "test") {
retStr.Content = new StringContent("Found Test", System.Text.Encoding.UTF8, "text/plain");
} else {
retStr.Content = new StringContent("Couldn't Find that test", System.Text.Encoding.UTF8, "text/plain");
}
return retStr;
}
所以现在在你的地址/ URI / ...
HTTP(S):// myURL / API / myController的/ VAR = GETNEW&测试=测试
结果:“发现测试”
HTTP(S):// myURL / API / myController的/ VAR = GETNEW&测试=任何
结果:“无法找到该测试”