Can I register multiple resources with one handler

2019-04-17 11:38发布

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.

2条回答
我只想做你的唯一
2楼-- · 2019-04-17 12:23

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.

查看更多
Animai°情兽
3楼-- · 2019-04-17 12:40

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.

查看更多
登录 后发表回答