3

本周我获准学习 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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="CompanyId" type="text" /></label><br />
    FirstName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="FirstName" type="text" /><br />
    LastName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="LastName" type="text" /><br />
    Middle Initial&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    **OTHER FIELDS**  
    <input type="submit" />
</form>

</body>

</html>

所有这一切都很好,只有 PUT 路由到 GET 服务。

我的目标当然是用新值更新客户行。

我没有显示客户存储库类,但它工作正常。我猜。我有一个具体的一般问题。

如何路由到 PUT 而不是 GET。是否有使用该服务进行更新的“最佳实践”。例如...... PUT 服务是否应该接收客户对象而不是所有值......然后回购代码获取记录并进行更新?

POST 方法(未显示)效果很好顺便说一句。它与 PUT 方法完全相同(接收客户对象等)

编辑

我也刚刚确定我尝试使用 DELETE http 方法也路由到 GET。这是一个简单的类型,甚至不从 Customer 继承。它只得到两个删除参数。现在我真的很困惑。

编辑 2

它似乎只是路由到返回具体类型的服务方法。例外是返回对象的 POST...Get 返回一个客户响应对象。获取客户返回一个客户(复数)响应对象并且可以工作。其余的服务方法是返回对象。是这样吗?

4

1 回答 1

8

正如 Eli 指出的那样,浏览器不支持 PUT/DELETE。应该能够让它与 ServiceStack 一起使用X-HTTP-Method-Override作为输入字段。@mythz 抢走了我的风头,并在这里增加了对它的支持(对于他打败我这件事并不痛苦)

此外,您的<form>方法是“获取”,它应该始终路由到您的 ServiceStack 的服务“获取”方法。

未经测试,但我认为这应该有效。

<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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="CompanyId" type="text" /></label><br />
    FirstName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="FirstName" type="text" /><br />
    LastName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="LastName" type="text" /><br />
    Middle Initial&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    **OTHER FIELDS**  
    <input type="submit" />
</form>
于 2013-03-15T17:04:07.323 回答