【问题标题】:Web API cant find apicontrollerWeb API 找不到 apicontroller
【发布时间】:2015-02-26 15:59:10
【问题描述】:

我正在尝试构建一个小型应用程序,一个使用实体框架获取数据并使用 web api 传递给 json 的 api,但得到错误:{"Message":"No HTTP resource was found that match请求 URI 'http://localhost:61267/api/GetCarousel'。","MessageDetail":"找不到与名为 'GetCarousel' 的控制器匹配的类型。"}

通话链接:http://localhost:61267/api/GetCarousel

当我戴上眼镜时,我了解到它似乎有很多解决方案,但似乎不适合我。

WebApiConfig 文件

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

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

                        );

        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
    }
}

GetCarousel.cs 控制器文件

这是在实体框架的帮助下从数据库生成的

namespace MustWebAPI.Controller
{
public class GetCarousel : ApiController
{
    private MustTestEntitie db = new MustTestEntitie();

    // GET: api/GetCarousel
    public IQueryable<GetCarousel_Result> GetGetCarousel_Result()
    {
        return db.GetCarousel_Result;
    }

    // GET: api/GetCarousel/5
    [ResponseType(typeof(GetCarousel_Result))]
    public IHttpActionResult GetGetCarousel_Result(int id)
    {
        GetCarousel_Result getCarousel_Result = db.GetCarousel_Result.Find(id);
        if (getCarousel_Result == null)
        {
            return NotFound();
        }

        return Ok(getCarousel_Result);
    }

    // PUT: api/GetCarousel/5
    [ResponseType(typeof(void))]
    public IHttpActionResult PutGetCarousel_Result(int id, GetCarousel_Result getCarousel_Result)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

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

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

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

        return StatusCode(HttpStatusCode.NoContent);
    }

    // POST: api/GetCarousel
    [ResponseType(typeof(GetCarousel_Result))]
    public IHttpActionResult PostGetCarousel_Result(GetCarousel_Result getCarousel_Result)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.GetCarousel_Result.Add(getCarousel_Result);
        db.SaveChanges();

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

    // DELETE: api/GetCarousel/5
    [ResponseType(typeof(GetCarousel_Result))]
    public IHttpActionResult DeleteGetCarousel_Result(int id)
    {
        GetCarousel_Result getCarousel_Result = db.GetCarousel_Result.Find(id);
        if (getCarousel_Result == null)
        {
            return NotFound();
        }

        db.GetCarousel_Result.Remove(getCarousel_Result);
        db.SaveChanges();

        return Ok(getCarousel_Result);
    }

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

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

【问题讨论】:

  • GetCarousel 是特定的吗?因为这对我来说看起来像是一个方法名,并且是一个糟糕的类名,因此是“GetGetCarousel”。类名中的下划线——GetCarousel_Result——违反Microsoft's naming rules

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


【解决方案1】:

您应该将类​​从 GetCarousel 重命名为 GetCarouselController,因为这是 Web api 路由的约定。

作为旁注,最好将其重命名为更合适的名称,例如“CarouselController”,通常“GetCarousel”更适合作为操作的名称,而不是控制器。 em>

【讨论】:

  • 更具体地说,他需要将类GetCarousel重命名为GetCarouselController,文件名无关
  • 同意...微妙但相关。已编辑。
  • 已删除并重新创建到:public class CarouselController : ApiController{} 仍然收到消息{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:61267/api/CarouselController'.","MessageDetail":"No type was found that matches the controller named 'CarouselController'."}
  • 网址将是...localhost:61267/api/Carousel ...您还需要重命名您的操作以遵循约定...
猜你喜欢
  • 2012-07-17
  • 1970-01-01
  • 2014-07-07
  • 1970-01-01
  • 2017-07-10
  • 1970-01-01
  • 2013-08-04
  • 2012-07-12
  • 1970-01-01
相关资源
最近更新 更多