webApi控制器路由不起作用

时间:2016-08-07 17:23:32

标签: c# entity-framework asp.net-mvc-4 asp.net-web-api routing

我有一个带有实体框架的webApi项目 我使用" webApi2控制器和动作,使用实体框架"添加我的一个实体Controller 但要求他们不工作

请求是:

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]工作,

是否需要使用web api2进行配置或代码?

谢谢!

1 个答案:

答案 0 :(得分:0)

您的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)