webApi控制器路由不';不起作用

本文关键字:不起作用 控制器 路由 webApi | 更新日期: 2023-09-27 18:01:08

我有一个带有实体框架的webApi项目我使用了"webApi2控制器与操作,使用实体框架"来添加我的一个实体控制器但是请求他们不起作用

请求是:

var baseApi = "/api/members/";
    return {
        getAllMembers: getAllMembers
    };
    function getAllMembers() {
        return $http({
            url: baseApi ,
            method: "GET"
        });
    }

webApi控制器代码为:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using sqs;
namespace sqs.Controllers
{
    public class membersController : ApiController
    {
        private sqsEntities db = new sqsEntities();
        // GET: api/members
        public IQueryable<member> Getmembers()
        {
            return db.members;
        }
        // GET: api/members/5
        [ResponseType(typeof(member))]
        public IHttpActionResult Getmember(long id)
        {
            member member = db.members.Find(id);
            if (member == null)
            {
                return NotFound();
            }
            return Ok(member);
        }
        // PUT: api/members/5
        [ResponseType(typeof(void))]
        public IHttpActionResult Putmember(long id, member member)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            if (id != member.AccountNumber)
            {
                return BadRequest();
            }
            db.Entry(member).State = EntityState.Modified;
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!memberExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return StatusCode(HttpStatusCode.NoContent);
        }
        // POST: api/members
        [ResponseType(typeof(member))]
        public IHttpActionResult Postmember(member member)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            db.members.Add(member);
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (memberExists(member.AccountNumber))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }
            return CreatedAtRoute("DefaultApi", new { id = member.AccountNumber }, member);
        }
        // DELETE: api/members/5
        [ResponseType(typeof(member))]
        public IHttpActionResult Deletemember(long id)
        {
            member member = db.members.Find(id);
            if (member == null)
            {
                return NotFound();
            }
            db.members.Remove(member);
            db.SaveChanges();
            return Ok(member);
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
        private bool memberExists(long id)
        {
            return db.members.Count(e => e.AccountNumber == id) > 0;
        }
    }
}

app_start中的webApi配置为:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace sqs
{
    public static class 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}/{name}",
                defaults: new { name = RouteParameter.Optional }
            );
        }
    }
}

没有[RoutePrefix]和[Route]工作,

使用webapi2是否需要任何配置或代码?

谢谢!

webApi控制器路由不';不起作用

您的Getmember方法参数是id

 public IHttpActionResult Getmember(long id)
        {
            member member = db.members.Find(id);
            if (member == null)
            {
                return NotFound();
            }
            return Ok(member);
        }

但您的路线配置是name

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{name}",
                defaults: new { name = RouteParameter.Optional }
            );

所以像一样改变你的路线

 routeTemplate: "api/{controller}/{id}",

还可以在方法之前使用Route属性为每个方法设置特定的路由

[Route("api/getmember/{id}")]
public IHttpActionResult Getmember(long id)