首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PUT/POST键/值集合到OpenRasta

PUT/POST键/值集合到OpenRasta
EN

Stack Overflow用户
提问于 2012-03-26 20:48:03
回答 1查看 479关注 0票数 0

应该如何实现OpenRasta处理程序,以便它可以接受基于URL模板的ID以及字典、哈希表或NameValueCollection (我不关心哪一个)?

我的URL-template是"/fielddata/{correlationId}“。

我的PUT信息是:

代码语言:javascript
复制
PUT http://myhost/fielddata/39950 HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 14

X=123&Y=Abc

我尝试过这样的处理程序:

代码语言:javascript
复制
public class FieldDataHandler
{
  public void Put(string correlationId, NameValueCollection data)
  {
  }
}

但是得到一个像这样的异常:

代码语言:javascript
复制
openrasta Verbose: 0 : Incoming host request for http://myhost/fielddata/39950
openrasta Verbose: 0 : Adding communication context data
openrasta Verbose: 0 : Found 1 operation(s) with a matching name.
openrasta Verbose: 0 : Found 0 operation(s) with matching [HttpOperation] attribute.
openrasta Information: 0 : Operation FieldDataHandler::Put(String correlationId, NameValueCollection data) selected with 2 required members and 0 optional members, with codec ApplicationXWwwFormUrlencodedKeyedValuesCodec with score 1,333333.
openrasta Error: 0 : An error has occurred and the processing of the request has stopped.

Exception:
System.InvalidOperationException: The operation is not ready for invocation.
   at OpenRasta.OperationModel.MethodBased.MethodBasedOperation.Invoke() in c:\Projects\OpenRasta\openrasta-stable\src\core\OpenRasta\OperationModel\MethodBased\MethodBasedOperation.cs:line 56
   at OpenRasta.OperationModel.Interceptors.OperationWithInterceptors.<Invoke>b__0() in c:\Projects\OpenRasta\openrasta-stable\src\core\OpenRasta\OperationModel\Interceptors\OperationWithInterceptors.cs:line 47
   at OpenRasta.OperationModel.Interceptors.OperationWithInterceptors.Invoke() in c:\Projects\OpenRasta\openrasta-stable\src\core\OpenRasta\OperationModel\Interceptors\OperationWithInterceptors.cs:line 52
   at OpenRasta.OperationModel.OperationExecutor.Execute(IEnumerable`1 operations) in c:\Projects\OpenRasta\openrasta-stable\src\core\OpenRasta\OperationModel\OperationExecutor.cs:line 14
   at OpenRasta.Pipeline.Contributors.OperationInvokerContributor.ExecuteOperations(ICommunicationContext context) in c:\Projects\OpenRasta\openrasta-stable\src\core\OpenRasta\Pipeline\Contributors\OperationInvokerContributor.cs:line 29
   at OpenRasta.Pipeline.PipelineRunner.ExecuteContributor(ICommunicationContext context, ContributorCall call) in c:\Projects\OpenRasta\openrasta-stable\src\core\OpenRasta\Pipeline\PipelineRunner.cs:line 187
openrasta Information: 0 : Executing OperationResult OperationResult: type=InternalServerError, statusCode=500.
EN

回答 1

Stack Overflow用户

发布于 2012-03-29 18:46:25

最终通过为特定于域的资源类型(而不是通用的NameValueCollection类型)创建一个新的编解码器来修复这个问题,然后通过其他方式执行form- up编码(反)序列化。

编解码器本身看起来像这样:

代码语言:javascript
复制
[MediaType("application/x-www-form-urlencoded")]
public class RestFieldDataCodec : IMediaTypeWriter, IMediaTypeReader
{
  public void WriteTo(object entity, IHttpEntity response, string[] codecParameters)
  {
    RestFieldData data = (RestFieldData)entity;

    using (TextWriter writer = new StreamWriter(response.Stream))
    {
      FormUrlEncodingSerializer serializer = new FormUrlEncodingSerializer(typeof(NameValueCollection));
      serializer.Serialize(writer, data.Data);
    }
  }


  public object ReadFrom(IHttpEntity request, IType destinationType, string destinationName)
  {
    using (TextReader reader = new StreamReader(request.Stream))
    {
      FormUrlEncodingSerializer serializer = new FormUrlEncodingSerializer(typeof(NameValueCollection));
      NameValueCollection keyValueData = (NameValueCollection)serializer.Deserialize(reader);
      return new RestFieldData
      {
        Data = keyValueData
      };
    }
  }


  public object Configuration { get; set; }
}

并且资源类型非常简单:

代码语言:javascript
复制
public class RestFieldData
{
  public NameValueCollection Data { get; set; }
}

FormUrlEncodingSerializer类来自https://github.com/JornWildt/Ramone

处理程序方法的结果如下:

代码语言:javascript
复制
public void Put(string correlationId, RestFieldData payload)
{
  ...
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9872406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档