Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to create page filter parameter in web api?

How to create page filter parameter in web api?

Scheduled Pinned Locked Moved ASP.NET
databasehelpquestionlinqjson
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    miss786
    wrote on last edited by
    #1

    I am writing to seek help in, how can I add page filter parameter to my method below, which can allow me to call a query string such as -- api/feed?name=st&page=2. I have tried using the page size property to make this work but the output would always give me 'no data response'. This is what I currently have and I am little stuck in how can I make this work. Please advice. Any solution would be most helpful. Many thanks.

    public HttpResponseMessage get([FromUri] Query query )
    {
    int pageSize = 10;
    int page = 0;

            IQueryable Data = null;
    
            if (!string.IsNullOrEmpty(query.name))
            {
                var ids = query.name.Split(',');
    
                var dataMatchingTags = db.data\_qy.Where(c => ids.Any(id => c.Name.Contains(id)));
    
                if (Data == null)
                    Data = dataMatchingTags;
                else
                    Data = Data.Union(dataMatchingTags);
            }
    
            if (Data == null) 
                Data = db.data\_qy;
    
            if (query.endDate != null)
            {
                Data = Data.Where(c => c.UploadDate <= query.endDate);
            }
    
            if (query.startDate != null)
            {
                Data = Data.Where(c => c.UploadDate >= query.startDate);
            }
    
            var totalCount = Data.Count();
           // var totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
    
           // var urlHelper = new UrlHelper(Request);
           // var prevLink = page > 0 ? urlHelper.Link("DefaultApi", new { page = page - 1 }) : "";
           // var nextLink = page < totalPages - 1 ? urlHelper.Link("DefaultApi", new { page = page + 1 }) : "";
    
            Data = Data.OrderByDescending(c => c.UploadDate);
    
            var data = Data.Skip(pageSize \* page).Take(pageSize).ToList();
    
            if (!data.Any())
            {
                var message = string.Format("No data found");
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
            }
    
            //return Request.CreateResponse(HttpStatusCode.OK, data);
            //return Request.CreateResponse(HttpStatusCode.OK, new { totalCount, totalPages, prevLink, nextLink, data });
            return Request.CreateResponse(HttpStatusCode.OK, new { totalCount, data });
        }
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups