How to use Breeze IQueryable with CORS?

I use the method of adding CORS handlers to my response, which is called by the client using Breeze.

You can learn more about how I got this working: The controller does not filter the data in the Breeze request in the DotNetNuke module

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

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

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

Original method (works)

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

My CORS shell method (not extensible or selectable)

 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; } 
0
source share
1 answer

I am going to comment mainly on the CORS question of your question. The part about $ expand and $ select is discussed in fooobar.com/questions/1489046 / .... In short, [Queryable] is a web API attribute that does not support $ expand and $ select. I think you need the [BreezeQueryable] attribute.

I can’t say for sure, but I don’t believe that the code you are showing is the right way to implement CORS for the web API. At least I have not seen this.

I know two ways; both include the addition of message handlers.

First, we did this in the Breeze Todo example; the second is CORS web API support, which is in transit.

The way we did this is simplified but effective. We are not talking about this because we intend to defer it to an approved web API when it arrives (I hope soon).

In the demo demo, find App_Start / BreezeSimpleCorsHandler.cs . You can simply copy it to your own App_Start folder without changes, except for the namespace.

Then your server should call it. In the Todo sample, we did this in BreezeWebApiConfig.cs, but you can put it in Global.asax or in anything that is part of the server load logic.

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

As it happens, someone tried Breeze with the upcoming CORS NuGet API package ... and found an error in Breeze. We must work to ... and we will do it. We really want this to be the way to go.

Until then, you can follow the Todo sample pattern.

+1
source

Source: https://habr.com/ru/post/1489043/


All Articles