简体   繁体   中英

WEB API 2 controller can't access the resource

I have a webforms project that supports MVC. I have created a WEB API 2 controller that used a model class which was generated using EF database first to convert SQL table into entity model. when I launch my application, the controller doesn't work as expected. I get the following when try to access the api:

HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

My Controller class is as following:

namespace YuClone.Controllers {
public class VideosController : ApiController
{
    private YuCloneContext db = new YuCloneContext();

    // GET: api/Videos
    public IQueryable<video> Getvideos()
    {
        return db.videos;
    }

    // GET: api/Videos/5
    [ResponseType(typeof(video))]
    public IHttpActionResult Getvideo(long id)
    {
        video video = db.videos.Find(id);
        if (video == null)
        {
            return NotFound();
        }

        return Ok(video);
    }

    // PUT: api/Videos/5
    [ResponseType(typeof(void))]
    public IHttpActionResult Putvideo(long id, video video)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (id != video.videoid)
        {
            return BadRequest();
        }

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

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

        return StatusCode(HttpStatusCode.NoContent);
    }

    // POST: api/Videos
    [ResponseType(typeof(video))]
    public IHttpActionResult Postvideo(video video)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.videos.Add(video);
        db.SaveChanges();

        return CreatedAtRoute("DefaultApi", new { id = video.videoid }, video);
    }

    // DELETE: api/Videos/5
    [ResponseType(typeof(video))]
    public IHttpActionResult Deletevideo(long id)
    {
        video video = db.videos.Find(id);
        if (video == null)
        {
            return NotFound();
        }

        db.videos.Remove(video);
        db.SaveChanges();

        return Ok(video);
    }

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

    private bool videoExists(long id)
    {
        return db.videos.Count(e => e.videoid == id) > 0;
    }
} }

What can be done to fix this?


Edit:

This is how the route is configured in WebApiConfig :

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

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

which is registered in application_start method in Global.aspx as :

RouteConfig.RegisterRoutes(RouteTable.Routes);

The URL I'm using to access the resource is :

http://localhost:5958/api/videos

您需要在Global.asax.cs注册WebAPI路由:

GlobalConfiguration.Configure(WebApiConfig.Register);

You are not registering the WebAPI routes.

RouteConfig.RegisterRoutes(RouteTable.Routes);

This code only registers MVC controller routes. For Web API, you need to register web api routes by calling the class you mentioned -

var config = GlobalConfiguration.Configuration;

WebApiConfig.Register(config); //register api routes
RouteConfig.RegisterRoutes(RouteTable.Routes);

This should work for you.

Make sure you are calling the RouteConfig.RegisterRoutes after the WebApiConfig.Register . Like this:

       System.Web.Http.GlobalConfiguration.Configure(WebApiConfig.Register);
       RouteConfig.RegisterRoutes(RouteTable.Routes);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM