0

I use a method to add CORS handlers to my response that is called by a client using Breeze.

You can read more about how I got that working here: Controller not filtering data in Breeze query in DotNetNuke Module

However, I noticed that while $filter works, $expand and $select do not.

So my question is: How can I use return a HttpResponseMessage Type and still use Breeze (I need to do this for CORS).

To prove this, I downloaded and changed the Todos sample:

Original method (works)

http://example/api/todos/todos?$select=isdone
[HttpGet]
public IQueryable<TodoItem> Todos()
{
    return _contextProvider.Context.Todos;
}

My method with CORS wrapper (does not expand or select)

http://example/api/todos/TodosCors?$select=isdone
[HttpGet]
[Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
public HttpResponseMessage TodosCors()
{
    var response = Request.CreateResponse(HttpStatusCode.OK, (IQueryable<TodoItem>)_contextProvider.Context.Todos);
    return ControllerUtilities.GetResponseWithCorsHeader(response);
}

    public static HttpResponseMessage GetResponseWithCorsHeader(HttpResponseMessage response)
    {
        response.Headers.Add("Access-Control-Allow-Origin", "*");
        return response;
    }
4

1 回答 1

1

我将主要评论您问题的CORS方面。关于 $expand 和 $select 的部分在您提到的 StackOverflow 问题中得到解决。简而言之,[Queryable]就是不支持 $expand 和 $select 的 Web API 属性。我想你想要那个[BreezeQueryable]属性。

我不能肯定地说,但我不相信你展示的代码是为 Web API 实现 CORS 的正确方法。至少我还没有看到它是这样做的。

我知道有两种方法;两者都涉及添加消息处理程序。

第一个是我们在 Breeze Todo 示例中的做法;第二个是即将推出的 Web API CORS 支持。

我们这样做的方式简单但有效。我们不谈论它,因为我们打算在它到达时推迟到批准的 Web API 方式(我希望很快)。

Todo 演示中,查找 App_Start/ BreezeSimpleCorsHandler.cs。您可以将其复制到您自己的 App_Start 文件夹中,除了命名空间之外没有任何更改。

然后你的服务器必须调用它。在 Todo 示例中,我们在BreezeWebApiConfig.cs中执行此操作,但您可以将其放在Global.asax或任何属于服务器启动逻辑的任何内容中。

      // 在此服务器上启用 CORS
      GlobalConfiguration.Configuration.MessageHandlers.Add(new BreezeSimpleCorsHandler());

碰巧,有人用即将推出的 Web API CORS NuGet 包尝试了 Breeze……并在 Breeze 中发现了一个错误。我们必须解决这个问题……而且我们会的。我们真的希望这种方式成为方式。

在此之前,您可以遵循 Todo 示例先例。

于 2013-07-05T00:12:42.320 回答