Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向ASP.NET Api添加排序、分页和筛选_Asp.net_Asp.net Mvc_Asp.net Web Api_Pagination_Filtering - Fatal编程技术网

向ASP.NET Api添加排序、分页和筛选

向ASP.NET Api添加排序、分页和筛选,asp.net,asp.net-mvc,asp.net-web-api,pagination,filtering,Asp.net,Asp.net Mvc,Asp.net Web Api,Pagination,Filtering,你能给我解释一下如何将排序、过滤和分页添加到我的ASP.NETWebAPI中,这样我就可以这样编写url了吗 http://localhost:8000/api/Meetings?sort_by=name&sort_type=asc&s=Joh&page=1&page_size=3 我有一个会议控制员: using System; using System.Collections.Generic; using System.Linq; using System.

你能给我解释一下如何将排序、过滤和分页添加到我的ASP.NETWebAPI中,这样我就可以这样编写url了吗

http://localhost:8000/api/Meetings?sort_by=name&sort_type=asc&s=Joh&page=1&page_size=3
我有一个
会议控制员

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebApplication3.Models;

namespace WebApplication3.Controllers
{
    [Route("api/Meetings")]
    [ApiController]
    public class MeetingsController : ControllerBase
    {
        private readonly MeetingContext _context;

        public MeetingsController(MeetingContext context)
        {
            _context = context;
        }

        //GET: api/Meetings
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Meeting>>> GetMeetings()
        {
            return await _context.Meetings.ToListAsync();
        }

        // GET: api/Meetings/5
        [HttpGet("{id}")]
        public async Task<ActionResult<Meeting>> GetMeeting(string id)
        {
            var meeting = await _context.Meetings.FindAsync(id);

            if (meeting == null)
            {
                return NotFound();
            }

            return meeting;
        }

        // PUT: api/Meetings/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutMeeting(string id, Meeting meeting)
        {
            if (id != meeting.Id)
            {
                return BadRequest();
            }

            _context.Entry(meeting).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MeetingExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/Meetings
        [HttpPost]
        public async Task<ActionResult<Meeting>> PostMeeting(Meeting meeting)
        {
            if (ModelState.IsValid)
            {
                Guid obj = Guid.NewGuid();
                meeting.Id = obj.ToString();
                _context.Meetings.Add(meeting);
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (MeetingExists(meeting.Id))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtAction(nameof(GetMeeting), new { id = meeting.Id }, meeting);
        }

        // DELETE: api/Meetings/5
        [HttpDelete("{id}")]
        public async Task<ActionResult<Meeting>> DeleteMeeting(string id)
        {
            var meeting = await _context.Meetings.FindAsync(id);

            if (meeting == null)
            {
                return NotFound();
            }

            _context.Meetings.Remove(meeting);
            await _context.SaveChangesAsync();

            return meeting;
        }

        private bool MeetingExists(string id)
        {
            return _context.Meetings.Any(e => e.Id == id);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Http;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.EntityFrameworkCore;
使用WebApplication3.Models;
命名空间WebApplication3.Controllers
{
[路线(“api/会议”)]
[ApiController]
公共类会议控制器:控制器数据库
{
私人只读会议上下文\u上下文;
公共会议控制器(会议上下文)
{
_上下文=上下文;
}
//获取:api/会议
[HttpGet]
公共异步任务GetMeetings()
{
return wait_context.Meetings.toListSync();
}
//获取:api/Meetings/5
[HttpGet(“{id}”)]
公共异步任务GetMeeting(字符串id)
{
var meeting=wait_context.Meetings.FindAsync(id);
如果(会议==null)
{
返回NotFound();
}
回程会议;
}
//PUT:api/Meetings/5
[HttpPut(“{id}”)]
公共异步任务PutMeeting(字符串id,会议)
{
if(id!=meeting.id)
{
返回请求();
}
_context.Entry(meeting.State=EntityState.Modified;
尝试
{
wait_context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
如果(!MeetingExists(id))
{
返回NotFound();
}
其他的
{
投掷;
}
}
返回NoContent();
}
//职位:宣传短片/会议
[HttpPost]
公开异步任务会后(会议)
{
if(ModelState.IsValid)
{
Guid obj=Guid.NewGuid();
meeting.Id=obj.ToString();
_上下文。会议。添加(会议);
}
尝试
{
wait_context.SaveChangesAsync();
}
捕获(DbUpdateException)
{
如果(会议存在(会议Id))
{
返回冲突();
}
其他的
{
投掷;
}
}
返回createDataAction(nameof(GetMeeting),new{id=meeting.id},meeting);
}
//删除:api/Meetings/5
[HttpDelete(“{id}”)]
公共异步任务DeleteMeeting(字符串id)
{
var meeting=wait_context.Meetings.FindAsync(id);
如果(会议==null)
{
返回NotFound();
}
_上下文。会议。删除(会议);
wait_context.SaveChangesAsync();
回程会议;
}
存在私有布尔会议(字符串id)
{
return _context.Meetings.Any(e=>e.Id==Id);
}
}
}
试试看:

这只是一个示例:

 //GET: api/Meetings
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Meeting>>> GetMeetings(string sort_by, string sort_type, string s, int page , int page_size)
        {
            IQueryable<Meeting> query = _context.Meetings;
            switch (sort_by)
            {
                case "name":
                    if (sort_type == "asc")
                    {
                        query = query.orderby(c => c.name);
                    }
                    else
                    {
                        query = query.OrderByDescending(c => c.name);
                    }

                    break;
                case "surname":
                    if (sort_type == "asc")
                    {
                        query = query.orderby(c => c.surname);
                    }
                    else
                    {
                        query = query.OrderByDescending(c => c.surname);
                    }

                    break;
                     //do more


            }
            //your search 
            if (!string.IsNullOrEmpty(s))
            {
           
                query = query.where(c => c.name.Contains(s));
            }




            return await query.Skip((page - 1)* page_size).Take(page_size).ToListAsync();
        }
//GET:api/Meetings
[HttpGet]
公共异步任务GetMeetings(字符串排序依据、字符串排序类型、字符串s、整型页面、整型页面大小)
{
IQueryable查询=_context.Meetings;
开关(按排序)
{
案例“名称”:
如果(排序类型=“asc”)
{
query=query.orderby(c=>c.name);
}
其他的
{
query=query.OrderByDescending(c=>c.name);
}
打破
“姓氏”一案:
如果(排序类型=“asc”)
{
query=query.orderby(c=>c.lasname);
}
其他的
{
query=query.OrderByDescending(c=>c.姓氏);
}
打破
//多做
}
//你的搜索
如果(!string.IsNullOrEmpty)
{
query=query.where(c=>c.name.Contains);
}
返回等待查询。跳过((第-1页)*页面大小)。获取(页面大小)。ToListSync();
}

您可以添加一个模型,以避免将每个参数分开。@Arcord他/她希望按如下查询字符串进行筛选:ToListSync()出现错误,IEnumerable不包含“ToListSync()”的定义。您仍然可以使用模型,只需使用“[FromQuery]”属性,以警告模型绑定器数据将来自查询参数。@Aska我更改这一行,请尝试:IQueryable query=\u context.Meetings;