Ignore OData EnableQuery attribute on condition after retrieving results - odata

I have an IActionResult service in an API controller which uses EnableQuery to allow for custom query string manipulations of results. The issue is that there is some custom validation in this IActionResult function (checking valid date of birth in query string) which is then returned to the user with information of the error they've made in input.
If that user added a query string while hitting some of this validation, instead of returning the error result, it is checking the query string and returning the following OData query validation:
The query specified in the URI is not valid. The property cannot be used in the query option
Is there a way for me to add a custom check in a function that inherits from EnableQueryAttribute, for example, if the result json contains 'error code', ignore all query strings?

Related

Get BreezeJS ODATA query parameters on server side to update entity during query

How do I get handle of the OData query parameters in a Brezze WebApi controller?
I have a endpoint where only a single entity will be queried at a time. The controller just calls a repository that returns a context.Set().AsQueryable();
I want to be able to get the entity ID out of the parameters and update(plain EF) a count property on it before querying it for the response.
Thanks
I found what I needed.
By adding a ODataQueryOptions<T> odataQueryOptions argument to the endpoint I was able to access the OData query parameters and get the ID I needed by getting this property:
var where = odataQueryOptions.RawValues.Filter;
Filter value in my case looks like this: Id eq 'dd3d6cb2-bc7a-467e-9730-c43c333b6fda'
Edit:
If you use .withParameters() on your breeze query object, you can pass additional parameters to your end-point without bothering to deal with OData.

Calling a web api method with custom object

Usually I send my post request with a custom parameter and a custom return and object using
HttpClientExtension.PostAsJsonAsync<T>
This allows my to call a post method with a custom object.
Now, I want to be able to send my custom object as a parameter and return value to a GET Method.
Lets say my method signature is
[HttpGet]
public MyMethodResponse MyMethod(MyMethodRequest request)
How can I send a request when I have an instance of MyMethodRequest ?
Thanks.
You need to encode MyMethodRequest onto the query string. You can either encode it as separate query string parameters or as a single one. You have handle the encoding yourself on the client side, remembering to URI-encode the parameters. Decoding is done using a custom ModelBinder or TypeConverter respectively. This article shows examples of binding a complex object on the query string.

Valence user search returns 404

The call executed is …/users/
with parameter orgDefinedId = “e5555555”.
As per documentation, an array of UserData should be returned.
In practice, when such user doesn’t exist, the call returns 404 "Resource Not Found" (not documented).
This is a documentation oversight. The answer is:
If you used the action with no query parameter filters, the action returns a paged result set containing the users it can find. If, because of the calling user's role permissions, the action can find no users, it returns with a 200 status code, but an empty result set.
If you used the action with a query parameter filter (userName or orgDefinedId), the action returns the matching user record(s) it can find; if it can find no matching user(s), the action returns with a 404 status code.
The documentation has been updated to clarify this.

MVC - Query string and action parameter issue

I have an action method with the following signature,
ActionResult Search(string searchQuery)
This gets called from a partial view on button submit from a form. Problem is, please look at the 2 patterns below. When I submit my search key from my page it uses the following url (suppose search key is tool)
Search/?searchQuery=tool
But then if I click on a tool then,
Search/tool?searchQuery=garden
Now my method is reading tool in the parameter instead of garden (which is expected of course). I presume this is to do with incorrect presentation of items from both the context of the item itself and that of search.
Is there a nice way of resolving this issue? I want to read the query string term and search for it from the main search context i.e. Search/?searchQuery=<term> no matter where I am.
To get the QueryString, in your controller you should write something like this:
var mystring =Request.QueryString["searchQuery"];
This will get the query string no matter where is placed in your url.
Rename the input to
ActionResult Search(string searchQuery)
The model binder will then deserialize the query string param to that input value. It will work for both route params and query string params.

DotNetOpenAuth - How to generate a multi-part request with some query string parameters?

I need to make oAuth multipart request message for the API which requires some query string parameters as well as Multipart Post Part.
But there is no PrepareAuthorizedRequest Method which accepts both IDictionary type query string parameters and MultipartPostPart fields.
What should I do?
Simply create a MessageReceivingEndpoint whose URI already includes those query string parameters.

Resources