3

我在 .net 服务中使用 OpenRasta 框架,并且在处理程序中有两种方法如下

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

我的配置如下所示

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/

上面的请求给了我 MyResource 的列表,与我为下面的请求得到的相同

HttpMethod: GET AcceptHeader: "application/json" URI: http://testDomain.com/MyResources/

将配置更改为

ResourceSpace.Has.ResourcesOfType<MyResource>()
          .AtUri("/MyResource/{Id}")
          .And.AtUri("/MyResource").Named("MyResource")
          .HandledBy<MyResourceHandler>()
          .AsJsonDataContract() 
          .And.AsXmlDataContract()

并在处理程序中进行适当的更改,即

[HttpOperation(HttpMethod.GET, ForUriName = "MyResource")]

OpenRasta 返回 415 http 状态码。

上面又不一致了。

对于与上述类似配置的我的其他资源,OpenRasta 抛出 403 http 状态代码

4

2 回答 2

1

第一种情况是正确的。您在两者之间共享一个处理程序。因此,当查看处理程序以选择方法时,有一个带有参数的候选者和一个没有参数的候选者。当您访问 /MyResource 时,它​​会找到处理程序并找到没有参数的方法。这是预期的行为。

在您的第二个配置中,那里缺少一些东西。415 是 OR 不理解请求数据。因为它是一个 GET,所以应该没有要处理的请求媒体类型。这将需要一个调试日志来检查发生了什么。您确定您的请求没有附带一些请求数据和 Content-Type 吗?

于 2012-07-24T13:37:47.310 回答
0

OpenRasta 的 GET 方面,我的想法很好,这是我仍在努力的 POST:

我只为 JSON 做了非常相似的事情,但这样做是这样的:

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

您可以在 Get 方法中使用默认参数处理带参数和不带参数。我认为从您的第二个配置中您缺少 ResourceType 中的 IList 因为非参数选项将返回一个列表。然后通过 ToList() 方法返回一个 IEnumerable 是不言而喻的。

这一切都假设您在“MyResource 列表”中使用 SQL-to-Linq,但我看不到您的代码的那部分以及它在做什么。如果不只是忽略我包含的私有函数并采取你自己的方法。

于 2012-12-07T12:12:09.613 回答