Web API有两个控制器,一个不起作用

时间:2014-04-18 18:56:38

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

我有一个带有两个控制器的web api。在localhost上都可以工作。在实时IIS 7.0上,一个工作,另一个返回"未找到与请求URI匹配的HTTP资源' * '。

有效的控制器是:

namespace DBTest.Controllers
{
  public class EmployeesController : ApiController
  {
    private GaryEntities1 db = new GaryEntities1();

    // GET api/Employees
    public IQueryable<EMPLOYEE> GetEMPLOYEES()
    {
        return db.EMPLOYEES;
    }

    // GET api/Employees/5
    [ResponseType(typeof(EMPLOYEE))]
    public IHttpActionResult GetEMPLOYEE(int id)
    {
        EMPLOYEE employee = db.EMPLOYEES.Find(id);
        if (employee == null)
        {
            return NotFound();
        }

        return Ok(employee);
    }

    // PUT api/Employees/5
    public IHttpActionResult PutEMPLOYEE(int id, EMPLOYEE employee)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (id != employee.EMPID)
        {
            return BadRequest();
        }

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

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

        return StatusCode(HttpStatusCode.NoContent);
    }

    // POST api/Employees
    [ResponseType(typeof(EMPLOYEE))]
    public IHttpActionResult PostEMPLOYEE(EMPLOYEE employee)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.EMPLOYEES.Add(employee);
        db.SaveChanges();

        return CreatedAtRoute("DefaultApi", new { id = employee.EMPID }, employee);
    }

    // DELETE api/Employees/5
    [ResponseType(typeof(EMPLOYEE))]
    public IHttpActionResult DeleteEMPLOYEE(int id)
    {
        EMPLOYEE employee = db.EMPLOYEES.Find(id);
        if (employee == null)
        {
            return NotFound();
        }

        db.EMPLOYEES.Remove(employee);
        db.SaveChanges();

        return Ok(employee);
    }

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

    private bool EMPLOYEEExists(int id)
    {
        return db.EMPLOYEES.Count(e => e.EMPID == id) > 0;
    }
  }
}

没有的是:

namespace DBTest.Controllers
{
  public class GPSController : ApiController
  {
    private GaryEntities1 db = new GaryEntities1();

    // GET api/GPS
    public IQueryable<GPS> GetGPS()
    {
        return db.GPS;
    }

    // GET api/GPS/5
    [ResponseType(typeof(GPS))]
    public IHttpActionResult GetGPS(int id)
    {
        GPS gps = db.GPS.Find(id);
        if (gps == null)
        {
            return NotFound();
        }

        return Ok(gps);
    }

    // PUT api/GPS/5
    public IHttpActionResult PutGPS(int id, GPS gps)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

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

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

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

        return StatusCode(HttpStatusCode.NoContent);
    }

    // POST api/GPS
    [ResponseType(typeof(GPS))]
    public IHttpActionResult PostGPS(GPS gps)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.GPS.Add(gps);
        db.SaveChanges();

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

    // DELETE api/GPS/5
    [ResponseType(typeof(GPS))]
    public IHttpActionResult DeleteGPS(int id)
    {
        GPS gps = db.GPS.Find(id);
        if (gps == null)
        {
            return NotFound();
        }

        db.GPS.Remove(gps);
        db.SaveChanges();

        return Ok(gps);
    }

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

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

这是WebApiConfig:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        // Configure Web API to use only bearer token authentication.
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

        // Web API routes
        config.MapHttpAttributeRoutes();

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

对我而言,它们看起来完全相同。我认为它必须是服务器上的IIS问题,因为它在localhost上工作正常。有人有什么想法吗?

0 个答案:

没有答案