api控制器方法是:
public IHttpActionResult GettblEmpleados(int page, int pageSize)
{
var query = db.SPEmpleadosIntranet().ToList();
var totalCount = query.Count();
var totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
var urlHelper = new UrlHelper(Request);
var prevLink = page > 0 ? urlHelper.Link("Empleados", new { page = page - 1, pageSize = pageSize }) : "";
var nextLink = page < totalPages - 1 ? urlHelper.Link("Empleados", new { page = page + 1, pageSize = pageSize }) : "";
var results = query
.Skip(pageSize * page)
.Take(pageSize)
.ToList();
return Ok(query);
}
我用来使用该服务的网址是:
但是,页面始终为0,则prevLink始终为空 http://localhost:12929/api/Empleados?page=0&pageSize=10
我得到的错误是:(抱歉框架以西班牙文为准)
Translatio说无法找到路径/ Empleados
No se encuentra ninguna ruta con el nombre'Empleados'enlacolecciónderutas。\ r \ nNombredelparámetro:name“
这是控制器
public class EmpleadosController : ApiController
{
这是globa.asax
protected void Application_Start()
{
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
UPDATE1
这是webapiconfig
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
答案 0 :(得分:1)
在您当前的网址中,您没有提及您的行为......
Controller / Action / Params
在您的webapiconfig中 - &gt;
routeTemplate: "api/{controller}/{action}/{id}"
http://localhost:12929/api/Empleados/GettblEmpleados?page=0&pageSize=10
答案 1 :(得分:1)
你需要告诉方法它匹配哪个Http方法。尝试使用以下属性进行装饰:
[HttpGet]
public IHttpActionResult GettblEmpleados(int page, int pageSize)
{
答案 2 :(得分:1)
由于您使用的是config.MapHttpAttributeRoutes();
,因此您可以按照以下方式使用Route Prefixes,因为您使用UrlHelper.Link Method,因此需要设置Route Name
[RoutePrefix("api/Empleados")]
public class EmpleadosController : ApiController {
[Route("", Name="Empleados")]
[HttpGet]
public IHttpActionResult GettblEmpleados(int page, int pageSize) {
var query = db.SPEmpleadosIntranet().ToList();
var totalCount = query.Count();
var totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
var urlHelper = new UrlHelper(Request);
var prevLink = page > 0 ? urlHelper.Link("Empleados", new { page = page - 1, pageSize = pageSize }) : "";
var nextLink = page < totalPages - 1 ? urlHelper.Link("Empleados", new { page = page + 1, pageSize = pageSize }) : "";
var results = query
.Skip(pageSize * page)
.Take(pageSize)
.ToList();
return Ok(query);
}
}