2012-03-26 3 views
0

사전, Hashtable 또는 NameValueCollection과 함께 URL 템플릿 기반 ID를 허용 할 수 있도록 OpenRasta 처리기를 구현하는 방법은 무엇입니까?OpenRasta에 PUT/POST 키/값 콜렉션

내 URL 템플릿은 "/ fielddata/{correlationId}"입니다.

내 PUT 메시지는 다음과 같습니다

PUT http://myhost/fielddata/39950 HTTP/1.1 
Content-Type: application/x-www-form-urlencoded 
Content-Length: 14 

X=123&Y=Abc 

는이 같은 핸들러 시도 :

public class FieldDataHandler 
{ 
    public void Put(string correlationId, NameValueCollection data) 
    { 
    } 
} 

을하지만이 같은 예외가 :

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. 

답변

0

에 의해이 고정 종료 됨 (일반적인 NameValueCollection 유형 대신) 도메인 특정 자원 유형에 대한 새 코덱을 작성한 다음 양식을 수행하십시오 다른 수단에 의한 직렬화 (de) 직렬화.

[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; } 
} 

을 그리고 자원 유형은 오히려 간단하다 :

코덱 자체는 다음과 같습니다

public class RestFieldData 
{ 
    public NameValueCollection Data { get; set; } 
} 

FormUrlEncodingSerializer 클래스는 핸들러 메소드는 다음과 같이 결국 https://github.com/JornWildt/Ramone

에서 :

public void Put(string correlationId, RestFieldData payload) 
{ 
    ... 
}