如何使用Breeze IQueryable与CORS? [英] How to use Breeze IQueryable with CORS?

查看:339
本文介绍了如何使用Breeze IQueryable与CORS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一种方法将CORS处理程序添加到客户端使用Breeze调用的响应中。



您可以阅读有关我在这里工作的更多信息: 控制器不会在DotNetNuke模块中的Breeze查询中过滤数据< a>



但是,我注意到当$ filter工作时, $ expand和$ select 因此我的问题是:如何使用返回一个HttpResponseMessage类型,并仍然使用Breeze(我需要这样做CORS)。

/ p>

为了证明这一点,我下载并更改了Todos示例:



/ strong>

  http:// example / api / todos / todos?$ select = isdone 



  [HttpGet] 
public IQueryable< TodoItem> Todos()
{
return _contextProvider.Context.Todos;
}

使用CORS包装器的我的方法(不扩展或选择) / strong>

  http:// example / api / todos / TodosCors?$ select = isdone 



  [HttpGet] 
[ 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;
}


解决方案

主要是关于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());

在这种情况下,有人尝试Breeze与即将到来的Web API CORS NuGet包...并发现了一个Bug在Breeze。我们必须通过...工作,我们会。



在这之前,您可以按照Todo的先例进行。


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

解决方案

I'm going to comment mainly on the CORS aspect of your question. The part about $expand and $select is addressed in the StackOverflow question to which you refer. In brief, [Queryable] is the Web API attribute which does not support $expand and $select. I think you want the [BreezeQueryable] attribute that does.

I can not say for sure but I do not believe the code you show is the proper way to implement CORS for the Web API. At least I've not seen it done this way.

There are two ways known to me; both involve adding message handlers.

The first is the way we did it in the Breeze Todo sample; the second is the with the Web API CORS support that is on the way.

The way we did it is simplistic but effective. We don't talk about it because we intend to defer to the the approved Web API way when it arrives (soon I hope).

In the Todo demo, look for App_Start/BreezeSimpleCorsHandler.cs. You can just copy it into your own App_Start folder with no changes except to the namespace.

Then your server has to call it. In the Todo sample we did so in the BreezeWebApiConfig.cs but you could put it in Global.asax or in anything that is part of the server boot logic.

      // CORS enabled on this server
      GlobalConfiguration.Configuration.MessageHandlers.Add(new BreezeSimpleCorsHandler());

As it happens, someone has tried Breeze with the forthcoming Web API CORS NuGet package ... and discovered a bug in Breeze. We have to work that through ... and we will. We really want that way to be THE way.

Until then, you can follow the Todo sample precedent.

这篇关于如何使用Breeze IQueryable与CORS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆