【发布时间】: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