realtek
realtek

Reputation: 841

Filtering and only display certain information using Web API

I am developing a website that is mostly written in asp.net and Javascript and I am using a lot of ajax on it to retreive and display information from a SQL database.

To perform all these operations I am using Web API as the communication path to perform server related tasks.

There are quite a few places on my website I will only want to display certain information. For example, I have the following route of which I may consume: api/customers/orders/order/5

This would retreive data for Order #5. However, what if some places on my website only need to display the Order # or Order Description or something? and what happens if I want to filter the database a bit more, to perhaps only display orders with specific information. By using the above URL it would return everything about the order to the web browser and seems a bit unnecessary.

If I have several needs for different filters then I don't understand how the routing would work for as some bits would be optional.

Any recommendations would be greatly appreciated! Or perhaps i'm missing something!

Thanks

Upvotes: 0

Views: 228

Answers (1)

Jalpesh Vadgama
Jalpesh Vadgama

Reputation: 14266

You can do something like following. You can create request filter like specified in below link.

public class RequestFilterAttribute : Attribute, IHasRequestFilter
{
     #region IHasRequestFilter Members

     public IHasRequestFilter Copy()
     {
       return this;
     }

     public int Priority
     {
        get { return -100; }
     }

     public void RequestFilter(IHttpRequest req, IHttpResponse res, object requestDto)
     {
        var query = req.QueryString["q"] ?? req.QueryString["query"];
        var limit = req.QueryString["limit"];

        var offset = req.QueryString["offset"];

        var user = requestDto as QueryBase;
        if (user == null) { return; }
        user.Query = query;
        user.Limit = limit.IsEmpty() ? int.MaxValue : int.Parse(limit);
        user.Offset = offset.IsEmpty() ? 0 : int.Parse(offset);
     }

      #endregion
}

.Net WebAPI URI convention for advanced searching /filtering

Upvotes: 1

Related Questions