如何实现使用ServiceStack删除服务电话 [英] How to implement Delete service call using ServiceStack

查看:150
本文介绍了如何实现使用ServiceStack删除服务电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个与使用ServiceStack REST服务实现的问题。


  1. 有关GET操作,我定义如下我的要求DTO:

      [路线(/客户/ {ID},动词=GET)]
    公共类GetCustomer:IReturn< GetCustomerResponse>
    {
        ....
        ....
    }


下面GetCustomer就是要求DTO和GetCustomerResponse的回应DTO。但是对于PUT / POST / DELETE操作,我只需要知道手术是否成功地得到了还是不承诺,如果不,那么什么是例外。那么,应该是POST / PUT我的要求DTO定义/ DELETE?如果它使用IReturnVoid如下图所示?

  [路线(/客户/ {ID},动词=删除)]
公共类DeleteCustomer:IReturnVoid
{
    ....
    ....
}

如果我必须使用IReturnVoid那么我怎么能检索犯我的操作可能发生的任何异常的信息?

在错误处理服务栈文件上写着,我引述如下


  

错误响应类型


  
  

这被当一个异常被抛出返回的错误响应
  变化是否常规名为{} RequestDto DTO响应
  存在与否。


  
  

如果它存在:


  
  

{该} RequestDto返回响应,无论服务
  方法的响应类型。如果{} RequestDto DTO的响应具有
  ResponseStatus属性,否则不填充ResponseStatus
  将被退回。 (如果您有装修{} ResponseDto响应
  类使用[DataContract] / [的DataMember]属性的属性,然后
  ResponseStatus也需要加以装饰,得到填充)。


  
  

否则,如果它不


  
  

一个通用ErrorResponse获取与填充的ResponseStatus返回
  属性。


  
  

在服务客户端透明地处理不同的错误响应
  类型,而对于无模式格式,如JSON / JSV /等没有实际
  在自定义返回ResponseStatus或之间明显的差异
  通用ErrorResponse - 因为它们都输出在相同的反应
  丝。


什么我没有从上面得到什么应该是我在我的服务实现删除方法的返回类型?我如何能实现我的删除方法没有定义删除响应DTO,但尚未我能够检索ErrorResponse'N例外信息?

<醇开始=2>
  • 是否有可能定义一个删除动词路线?我有以下的实现。

  • 路线:

      [路线(/ DeleteCustomer / {ID},动词=删除)]
    公共类DeleteCustomer:IReturn&LT; D​​eleteCustomerResponse&GT;
    {
        公众诠释ID {搞定;组; }
    }

    方法实现:

     公共DeleteContactResponse删除(DeleteContact要求)
    {
        .....
    }

    但每当我把这个删除使用我的客户,我总是得到NOTFOUND异常。我尝试不同的客户端,但所有我得到404错误。

    其中引用链接提供非常久远Servicestack文件一起重用GET和删除动词。

    表明并不是所有的浏览器都支持删除操作。

    所以,我不知道如何操作删除应该实施?


    解决方案

    我得到了我的第二个问题的修补程序从以下两个链接:
    1. 链接1
    2. <一个href=\"http://forums.asp.net/t/1961593.aspx?DELETE%20PUT%20verbs%20result%20in%20404%20Not%20Found%20in%20WebAPI%20only%20when%20running%20locally\"相对=nofollow>链路2

    我不完全理解这个修复,但做上述修改工作对我来说,现在我可以从任何客户端删除功能。

    有关问题1,请参阅下面详细@mythz的答复。

    I have couple of questions related to REST service implementation using ServiceStack.

    1. For GET operation, I define my request DTO as below :

      [Route("/Customer/{ID}", Verbs = "GET")]
      public class GetCustomer : IReturn<GetCustomerResponse>
      {
          ....
          ....
      }
      

    Here "GetCustomer" is request DTO and "GetCustomerResponse" is response DTO. But for PUT/POST/DELETE operation, I just need to know whether operation got committed successfully or not and if 'not' then what is the exception. So what should be my request dto definition for POST/PUT/DELETE? Should it use IReturnVoid as shown below?

    [Route("/Customer/{ID}", Verbs = "DELETE")]
    public class DeleteCustomer : IReturnVoid
    {
        ....
        ....
    }
    

    If I have to use IReturnVoid then how I can retrieve any exception information that might occur on committing my operation?

    In the error handling document for service stack it is written and I quote below

    Error Response Types

    The Error Response that gets returned when an Exception is thrown varies on whether a conventionally-named {RequestDto}Response DTO exists or not.

    If it exists:

    The {RequestDto}Response is returned, regardless of the service method's response type. If the {RequestDto}Response DTO has a ResponseStatus property, it is populated otherwise no ResponseStatus will be returned. (If you have decorated the {ResponseDto}Response class and properties with [DataContract]/[DataMember] attributes, then ResponseStatus also needs to be decorated, to get populated).

    Otherwise, if it doesn't:

    A generic ErrorResponse gets returned with a populated ResponseStatus property.

    The Service Clients transparently handles the different Error Response types, and for schema-less formats like JSON/JSV/etc there's no actual visible difference between returning a ResponseStatus in a custom or generic ErrorResponse - as they both output the same response on the wire.

    What I'm not getting from above is what should be the return type for my Delete method in my service implementation? How I can implement my delete method without defining delete response DTO but yet I'm able to retrieve 'ErrorResponse' n exception info?

    1. Is it possible to define route with "DELETE" verb? I have following implementation.

    Route:

    [Route("/DeleteCustomer/{ID}", Verbs = "DELETE")]
    public class DeleteCustomer : IReturn<DeleteCustomerResponse>
    {
        public int ID { get; set; }
    }
    

    Method implementation:

    public DeleteContactResponse Delete(DeleteContact request)
    {
        .....
    }
    

    But whenever I call this delete using my client, I always get "NotFound" exception. I tried different clients but with all I get 404 error.

    One of the reference link available alongwith Servicestack document reuses the "GET" and "DELETE" verb together.

    Another link suggests not all browsers support delete operation.

    So I wonder how Delete operation should be implemented?

    解决方案

    I got the fix for my 2nd question from following two links : 1. Link1 2. Link2

    I don't fully understand this fix but doing above changes worked for me and now I can call Delete function from any clients.

    For 1st question, pls refer @mythz 's reply below in detail.

    这篇关于如何实现使用ServiceStack删除服务电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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