Reputation: 769
I'm using OpenRasta framework in a .net service and I have two methods as below in the handler
public OperationResult Get(int Number)
{
// Do some operation and get an entity
return new OperationResult.OK(Single-MyResource);
}
public OperationResult GetQ()
{
// Do some operation and get an entity
return new OperationResult.OK(List-Of-MyResource);
}
My configuration looks like below
ResourceSpace.Has.ResourcesOfType<MyResource>()
.AtUri("/MyResource/{Id}")
.And.AtUri("/MyResource")
.HandledBy<MyResourceHandler>()
.AsJsonDataContract()
.And.AsXmlDataContract()
ResourceSpace.Has.ResourcesOfType<IList<MyResource>>()
.AtUri("/MyResources")
.HandledBy<MyResourceHandler>()
.AsJsonDataContract()
.And.AsXmlDataContract();
HttpMethod: GET AcceptHeader: "application/json" URI: http://testDomain.com/MyResource/
The above request gives me the List of MyResource , same as what i get for the below Request
HttpMethod: GET AcceptHeader: "application/json" URI: http://testDomain.com/MyResources/
After changing Configuration to
ResourceSpace.Has.ResourcesOfType<MyResource>()
.AtUri("/MyResource/{Id}")
.And.AtUri("/MyResource").Named("MyResource")
.HandledBy<MyResourceHandler>()
.AsJsonDataContract()
.And.AsXmlDataContract()
and making appropriate change in handler i.e
[HttpOperation(HttpMethod.GET, ForUriName = "MyResource")]
OpenRasta returns 415 http Status Code.
The above is not consistent again.
For my other Resource for similar configuration as above OpenRasta throws 403 http Status Code
Upvotes: 3
Views: 286
Reputation: 1068
The GET side of OpenRasta, I have got pretty well in my mind, It's the POST I still struggle with:
I've done really similar things to you for JSON only, but done it like this:
ResourceSpace.Has.ResourcesOfType<IList<MyResource>>()
.AtUri("/myresource").And
.AtUri("/myresource/{id}").HandledBy<ResourceHandler>().AsJsonDataContract();
[HttpOperation(HttpMethod.GET)]
public IEnumerable<MyResource> Get(int id = 0)
{
if (id == 0)
return Context.Set<MyResource>().ToList();
else
return GetMyResourceMethod(id).ToList();
}
private IQueryable<MyResource> GetMyResourceMethod(int id)
{
var myresource = from resource in Context.Set<MyResource>()
where resource.MyResourceId == id
select resource;
return myresource;
}
You can handle both with and without parameters with a default parameter in your Get method. I think from your second configuration your missing the IList from your ResourceType because the non parametered option will return a list. It is then impreitive to return a IEnumerable via the ToList() method.
This all assumes your using SQL-to-Linq in your 'List-Of-MyResource', but I can't see that part of your code and what it is doing. If not just ignore the private function I've included and take your own approach.
Upvotes: 0
Reputation: 6766
The first case is correct. You share a handler between the two. As such, when the handler is looked at to select a method, there is one candidate with a parameter and one without. When you to to /MyResource, it finds the handler and find the method that has no parameter. This is expected behavior.
In your second configuration, there's something missing there. A 415 is when the request data is not understood by OR. As it's a GET, there should be no request media type to deal with. This one will require a debug log to check what is going on. Are you sure your request is not coming with some request data and a Content-Type?
Upvotes: 1