我生成一个实体框架API控制器,现在我想新的方法添加到它:ASP.NET实体框架API控制器添加方法不工作
[ResponseType(typeof(LCPreview))]
public IHttpActionResult ValidateEmail(string email)
{
LCPreview lCPreview = db.Data.Find(5);
if (lCPreview == null)
{
return NotFound();
}
return Ok(lCPreview);
}
但是当我运行此,我得到这个错误:
The request is invalid. The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Http.IHttpActionResult GetLCPreview(Int32)' in 'Astoria.Controllers.PreviewLCAPIController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
你检查了从db.Data.Find调用返回的对象吗?它可能对id字段有一个空值。 –
我放了一个断点,它没有在所有 – user979331
路线冲突中碰到这个新方法。它正在击中符合请求的另一条路线。显示如何配置您的路线设置。并请求url – Nkosi