【发布时间】:2017-11-29 04:35:01
【问题描述】:
我正在使用 Web Api 2 框架来为 Angular 网站提供 HTTP 服务。我关注了官方的guide,一切看起来都很正常,除了当我从 POSTMAN 调用 GET、POST... 方法时,结果总是一样的:The resource cannot be found.
以下是路由配置:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
控制器的代码(我的控制器称为EndpointModelsController)是Visual Studio 自动生成的代码。
我搜索了一个解决方案,并尝试在此guide 中使用路由属性,但结果是相同的。
在我的情况下,Web 服务在端口 54800 上运行,并且我正在使用 IISEXPRESS:一个 URL 示例可能是这个 http://localhost:54800/api/EndpointModels 用于不带任何参数的 GET 请求。
是网址错误还是您认为问题出在代码中?可能是什么原因?
谢谢。
[编辑]
这是控制器的代码:我只添加了[HttpGet] 属性。
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using CodeProbe.WebApp.Models;
namespace CodeProbe.WebApp.Controllers
{
public class EndpointModelsController : ApiController
{
private EndpointContext db = new EndpointContext();
// GET: api/EndpointModels
[HttpGet]
public IQueryable<EndpointModels> GetEndPoint()
{
return db.EndPoint;
}
// GET: api/EndpointModels/5
[ResponseType(typeof(EndpointModels))]
[HttpGet]
public async Task<IHttpActionResult> GetEndpointModels(string id)
{
EndpointModels endpointModels = await db.EndPoint.FindAsync(id);
if (endpointModels == null)
{
return NotFound();
}
return Ok(endpointModels);
}
// PUT: api/EndpointModels/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutEndpointModels(string id, EndpointModels endpointModels)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != endpointModels.ServiceName)
{
return BadRequest();
}
db.Entry(endpointModels).State = EntityState.Modified;
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!EndpointModelsExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/EndpointModels
[ResponseType(typeof(EndpointModels))]
public async Task<IHttpActionResult> PostEndpointModels(EndpointModels endpointModels)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.EndPoint.Add(endpointModels);
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (EndpointModelsExists(endpointModels.ServiceName))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = endpointModels.ServiceName }, endpointModels);
}
// DELETE: api/EndpointModels/5
[ResponseType(typeof(EndpointModels))]
public async Task<IHttpActionResult> DeleteEndpointModels(string id)
{
EndpointModels endpointModels = await db.EndPoint.FindAsync(id);
if (endpointModels == null)
{
return NotFound();
}
db.EndPoint.Remove(endpointModels);
await db.SaveChangesAsync();
return Ok(endpointModels);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool EndpointModelsExists(string id)
{
return db.EndPoint.Count(e => e.ServiceName == id) > 0;
}
}
}
【问题讨论】:
-
看来一切都是正确的。您能否验证您是否有该控制器中任何方法的 Get 作为前缀或 HTTPGet 属性装饰的方法。
-
理论上如果我的方法以
Get开头,它必须被视为一个GET请求。只是为了确保我已经添加了 HTTPGet 属性并尝试更改路由(使用 Route 属性),但问题仍然存在。 -
嗨,Marco,我已经从头开始尝试过,它没有遇到任何问题。你能分享你在控制器中编写的代码吗?
-
如果你传递了“id”,它是否有效,例如localhost:54800/api/EndpointModels/100 OR localhost:54800/api/EndpointModelsController/100
-
感谢您的回答。无论如何,它甚至不适用于 id。
标签: c# routing entity-framework-6 asp.net-web-api2