Can I register multiple resources with one handler and one URI in Open Rasta?

故事扮演 提交于 2019-12-22 01:43:45

问题


I want to register mutliple resources with one handler and one URI.so want to confirm that is this possible in open rasta. I have gone through a bit of websites regarding open rasta but couldn't able to conclude that whether this is possible or not?

  ResourceSpace.Has
           .ResourcesOfType<Request>()
           .AtUri("/processing")
           .HandledBy<SomeHandler>()
           .TranscodedBy<SomeCodec>();    

I need to handle all the request which are derived from the base class "Request". It would be great if some body could actually shed light on this.


回答1:


So if everything is a request and you want to tunnel stuff, you can, using the registration you have provided.

The matching will be done based on the most approaching type, so

public void Post(SpecificRequest specificRequest) { /*handles the SpecificRequest type */ }
public void Post(Request myRequest) { /* handles the default case */ }

That said, be aware that in ReSTful system we tend to try and identify different things with different URIs, leading to one registration per type.




回答2:


In my fairly limited experience of OpenRasta you can use the .And method to add additional .AtUri resource locations.

I.e. in your case

ResourceSpace.Has
       .ResourcesOfType<Request>()
       .AtUri("/processing").And.AtUri("/processing/{processid}")
       .HandledBy<SomeHandler>()
       .TranscodedBy<SomeCodec>();  

where the {curley brackets} specify the input parameter of your Handler method i.e.:

public class SomeHandler
{
    public Request Get(int processid = 0) //specify a default value for the uri case /processed
    {
        if (processid == 0)
            return Context.Set<Request>().ToList(); //Context comes from my DbContext derived class which is part of my entity model.
        else
            return GetRequestFromProcessId(processid) //this is a private method in your handler class using Linq to SQL to retreive the data your interested in.  I can't see your handler so I'm making it up.
     }
 } 

I found in the OpenRasta documentation a note which specifed that you MUST have destinct (only one) ResourceSpace definition for each matching Type and Handler. In other words you must not duplicate the same ResourceOfType class with the same HandledBy handler class. I tested this and it is the case and perhaps why you are asking the question in the first place.

N.b. this code is completely untested, I've just taken the patern of what I've written and substituted in your classes and uri where known. This also assumes you are trying to retrieve data from a HTTP GET verb. The other contributer, went down the POST route, but you haven't specified.



来源:https://stackoverflow.com/questions/8615720/can-i-register-multiple-resources-with-one-handler-and-one-uri-in-open-rasta

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!