C#Web API无法处理简单的MVC5应用程序

时间:2015-09-28 16:04:32

标签: c# asp.net-web-api2

我正在尝试使用Web API和MVC5的一个简单示例,但到目前为止,它无法正常工作。我在Visual Studio 2013上创建了一个简单的MVC5应用程序,它显示了一个表格内容。我想为此添加Web API。我添加了一个Web API控制器,它似乎添加了所有必需的依赖项。但是,当我运行Web API部分时,我得到了404.我很确定问题出在下列文件之一。

Global.aspx:

    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }

RouteConfig.cs:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "MountJoy", action = "Index", id = UrlParameter.Optional }
        );
    }

WebApiConfig.cs:

    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

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

自动生成的控制器:

public class RemoteController : ApiController
{
    private MountJoyDBEntities db = new MountJoyDBEntities();

    // GET api/Remote
    public IQueryable<Inmate> GetInmates()
    {
        return db.Inmates;
    }

    // GET api/Remote/5
    [ResponseType(typeof(Inmate))]
    public IHttpActionResult GetInmate(int id)
    {
        Inmate inmate = db.Inmates.Find(id);
        if (inmate == null)
        {
            return NotFound();
        }

        return Ok(inmate);
    }

    // PUT api/Remote/5
    public IHttpActionResult PutInmate(int id, Inmate inmate)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (id != inmate.Id)
        {
            return BadRequest();
        }

        db.Entry(inmate).State = EntityState.Modified;

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!InmateExists(id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return StatusCode(HttpStatusCode.NoContent);
    }

    // POST api/Remote
    [ResponseType(typeof(Inmate))]
    public IHttpActionResult PostInmate(Inmate inmate)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.Inmates.Add(inmate);
        db.SaveChanges();

        return CreatedAtRoute("DefaultApi", new { id = inmate.Id }, inmate);
    }

    // DELETE api/Remote/5
    [ResponseType(typeof(Inmate))]
    public IHttpActionResult DeleteInmate(int id)
    {
        Inmate inmate = db.Inmates.Find(id);
        if (inmate == null)
        {
            return NotFound();
        }

        db.Inmates.Remove(inmate);
        db.SaveChanges();

        return Ok(inmate);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }

    private bool InmateExists(int id)
    {
        return db.Inmates.Count(e => e.Id == id) > 0;
    }
}

0 个答案:

没有答案