ServiceStack 路由设计 [英] ServiceStack Route design

查看:46
本文介绍了ServiceStack 路由设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这 3 条路线相同吗?通常首选哪一个?

Are these 3 routes the same? Which one is normally preferred?

[Route("/todo/{id}", "DELETE")]
[Route("/todo/delete","POST")]
[Route("/todo/delete/{id}","GET")]
public class DeleteTodo : IReturnVoid
{
    public int Id { get; set; }
}

非常感谢...

推荐答案

首选路由实际上是将 Id 包含在 pathinfo 中,因为 DELETE 请求没有您可以提交此信息的 HTTP 请求正文,例如:

The preferred route is actually to include the Id in the pathinfo since DELETE requests don't have a HTTP Request Body you can submit this info on, e.g:

[Route("/todo/{id}", "DELETE")]
public class DeleteTodo : IReturnVoid
{
    public int Id { get; set; }
}

出于实际原因,您可能希望允许 POST 执行 DELETE,因为我的默认浏览器(和某些代理)不允许发送 DELETE 请求.

For pragmatic reasons you may want to allow a POST to do the DELETE since browsers my default (and some proxies) don't allow sending of DELETE Requests.

[Route("/todo/{id}/delete", "POST")]
public class DeleteTodo : IReturnVoid
{
    public int Id { get; set; }
}

您可以在 Ajax 或 jQuery 中模拟 DELETE 请求,方法是在 Ajax 调用中添加 X-Http-Method-Override HTTP 请求标头或作为字段FormData 或 QueryString,例如

You can simulate a DELETE request in Ajax or jQuery by adding the X-Http-Method-Override HTTP Request header in your Ajax call or as a field in your FormData or QueryString, e.g.

POST /todo/1
X-Http-Method-Override=DELETE 

或嵌入在 HTML FormData 中,例如:

or embedded in the HTML FormData like:

<form action="/todo/1" method="POST">
   <input type="hidden" name="X-Http-Method-Override" value="DELETE"/>
</form>

虽然重要的是不允许通过 GET 进行 DELETE,因为根据合约 GET 应该没有副作用,因此可以安全地被 HTTP 中间件(如代理等)缓存和重放.

Though it's important not to allow DELETE's via GET as by contract GET's should have no side-effects so are safe to be cached and replayed by HTTP middle-ware like proxies, etc.

这篇关于ServiceStack 路由设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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