- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
本周我获准学习 ServiceStack。我喜欢它。这是一个了不起的框架。但是我遇到了一种情况,我无法获得一个相当直接的例子来工作。 (虽然它诚然不如示例那么简单,但可能是一个更现实的示例。)
提前为这个冗长的问题道歉。
我有一个简单的 DTO,可以映射到这样的数据库...
[Description("Customer")]
[Alias("Customers")]
public class Customer : IHasId<int>
{
[Alias("Id")]
[AutoIncrement]
public int Id { get; set;}
[Required]
public int CompanyId { get; set;}
[Required]
public string FirstName { get; set;}
[Required]
public string LastName { get; set;}
public string MiddleInitial { get; set;}
public string EmployerName { get; set;}
public string ServiceLocationDescription { get; set;}
public string Street1 { get; set;}
public string Street2 { get; set;}
public string City { get; set;}
public string State { get; set;}
public string Zip { get; set;}
[Required]
public string Phone { get; set;}
public string Fax { get; set;}
[Required]
public string EmailAddress { get; set;}
}
}
我还创建了如下所示的请求 DTO...
//request dto
[Route("/customers/{companyId/customer/{customerId}", "GET")]
public class GetCustomer : Customer
{
}
[Route("/customers/{companyId}/customer/{customerId}", "PUT")]
public class UpdateCustomer : Customer
{
}
我意识到路由是一样的……这可能是问题所在……但我指定了不同的 http 方法……
我终于有了一个看起来像这样的服务......
public CustomerResponse Get(GetCustomer request)
{
return new CustomerResponse { Customer = customerRepository.GetCustomer(request.CustomerId), };
}
public object Put(UpdateCustomer request)
{
customerRepository.UpdateCustomer(request);
return new HttpResult
{
StatusCode = HttpStatusCode.NoContent,
Headers = {
{ HttpHeaders.Location, this.RequestContext.AbsoluteUri.CombineWith(request.Id.ToString()) }
}
};
}
所以为了测试它,我创建了以下简单的 html...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>
<body>
<form action="http://localhost:8080/co/1/customers/1000" method="get">
<br />
<label id="Label1">CompanyId
<input name="CompanyId" type="text" /></label><br />
FirstName
<input name="FirstName" type="text" /><br />
LastName
<input name="LastName" type="text" /><br />
Middle Initial
**OTHER FIELDS**
<input type="submit" />
</form>
</body>
</html>
所有这一切都很好,只有 PUT 路由到 GET 服务。
我的目标当然是用新值更新客户行。
我没有显示客户存储库类,但它工作正常。我猜。我有一个具体的一般性问题。
如何路由到 PUT 而不是 GET。是否有使用该服务进行更新的“最佳实践”。例如...是否应该 PUT 服务不接收客户对象,而是接收所有值...然后 repo 代码获取记录并进行更新?
顺便说一句,POST 方法(未显示)效果很好。它与 PUT 方法完全一样(接收客户对象等)
编辑
我也刚刚确定我尝试使用 DELETE http 方法也路由到 GET。这是一个简单的类型,甚至不从 Customer 继承。它只得到两个删除参数。现在我真的很困惑。
编辑 2
似乎只是路由到返回具体类型的服务方法。异常(exception)是返回对象的 POST...Get 返回一个客户响应对象。获取客户返回一个客户(复数)响应对象并且可以工作。其余的服务方法是返回对象。是这样吗?
最佳答案
正如 Eli 指出的,浏览器不支持 PUT/DELETE。应该能够使用 X-HTTP-Method-Override
让它与 ServiceStack 一起工作作为输入字段。 @mythz 抢了我的风头并增加了对它的支持 here (对他把我打到这个地步并不痛苦)
还有你的<form>
方法是 'get',它应该始终路由到您的 ServiceStack 的 Service 'Get' 方法。
未经测试,但我认为这应该可行。
<form action="http://localhost:8080/co/1/customers/1000" method="POST">
<br />
<input name="X-HTTP-Method-Override" type="hidden" value="PUT" />
<label id="Label1">CompanyId
<input name="CompanyId" type="text" /></label><br />
FirstName
<input name="FirstName" type="text" /><br />
LastName
<input name="LastName" type="text" /><br />
Middle Initial
**OTHER FIELDS**
<input type="submit" />
</form>
关于servicestack - 为什么servicestack服务路由到GET而不是PUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15435042/
我正在处理批处理作业,以通过 HTableInterface 将一批 Put 对象处理到 HBase 中。有两个 API 方法,HTableInterface.put(List) 和 HTableIn
我想转置curl命令(将本地文件上传到机架空间) curl -X PUT -T screenies/hello.jpg -D - \ -H "X-Auth-Token: fc81aaa6-98a1-9
我认为执行 puts #{a} 会产生与 puts a 相同的输出,但发现情况并非如此。考虑: irb(main):001:0> a = [1,2] => [1, 2] irb(main):002:0
我尝试在我的一个移动应用程序中使用这个cordova后台服务插件(我正在使用名为Appery io的基于网络的移动应用程序开发平台)。我已经能够让相当多的 cordova/phonegap 插件正常工
传奇有多个 put。 export function* changeItemsSaga(action) { const prerequisite1 = yield select(prerequ
我正在从工作正常的 jquery $.ajax 切换到使用 AngularJS $http.put 来访问 Restful API。 我可以进行 API 调用,但 PUT 数据未发送 - 因此我的 A
我的 express 里有这个 router.put('/admin/profile?:id/actions', (req, res) => { console.log(req.body.action
我正在浏览 Ruby tutorial , 并了解到代码 puts 'start' puts puts 'end' 会输出三行,但是下面的代码 puts 'start' puts [] puts 'e
我的目标是使用 TreeMap 使 Box 键对象按 Box.volume 属性排序,同时能够将键按 Box.code 区分。在 TreeMap 中不可以吗? 根据下面的测试 1,HashMap pu
我一直在尝试使用HBase客户端库将记录列表插入HBase。 它适用于Table或HTable中的单个Put(不建议使用),但不能识别List(Puts) 错误说:预期:util.List,但是Lis
我正在使用 Google-guava-cache。将其定义为: Cache myCache= CacheBuilder.newBuilder().maximumSize(100).build();
我只是想知道threadContext.put和MDC.put之间的区别。因为,我相信两者都在做相同的操作。 最佳答案 Log4j 2 continues with the idea of the M
我的 GAE 应用程序上有一些模型,并且我已覆盖 put()关于其中一些。当我调用db.put()时有了这些实体的列表,是否可以保证覆盖 put()每个实体都会被调用? 我现在看到的是实体只是被保存而
module Lab def self.foo puts 'foo from lab' end end module M def foo puts 'foo from mo
我正在查看示例 abo3.c来自 Insecure Programming我并没有在下面的例子中摸索类型转换。有人可以启发我吗? int main(int argv,char **argc) {
我正在 symfony2.4 和 angularjs 中构建应用程序。在 Angular 中我创建了资源: app.factory('Tasks', ['$resource', function($r
到处都说[看了很多帖子] PUT 是幂等的,意味着具有相同输入的多个请求将产生与第一个请求相同的结果。 但是,如果我们使用 POST 方法发出具有相同输入的相同请求,那么它又将表现为 PUT。 那么,
这里是WebApiConfig.cs中的路由配置: config.Routes.MapHttpRoute( name: "DefaultApiPut", routeTemplate:
这两种方法有什么区别: getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke( "pressed F10"),
由于匿名 block 和散列 block 看起来大致相同。我正在玩它。我做了一些严肃的观察,如下所示: {}.class #=> Hash 好的,这很酷。空 block 被视为Hash。 print{
我是一名优秀的程序员,十分优秀!