我们正在为SQL Server表开发API。 WebApiConfig类如下:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<CRMUser>("User");
config.MapODataServiceRoute(routeName:"ODataRoute",
routePrefix: null,
model: builder.GetEdmModel());
}
}
控制器如下:
public class UsersController : ODataController
{
// GET api/<controller>
OnlineStoreEntities db = new OnlineStoreEntities();
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
public IEnumerable<CRM_vwUser> Get()
{
return db.CRM_vwUser;
}
// GET api/<controller>/5
public string Get(int id)
{
return "value";
}
// POST api/<controller>
public void Post([FromBody]string value)
{
}
// PUT api/<controller>/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/<controller>/5
public void Delete(int id)
{
}
}
但是浏览该网址“ http://localhost:50246/User”时出错。错误如下:
找不到与请求URI'http://localhost:50246/user'相匹配的HTTP资源。 找不到与名为“用户”的控制器匹配的类型。
答案 0 :(得分:1)
您的控制器名称为UsersController
(带有S),并且在您的URL中同时设置了../User
和
builder.EntitySet<CRMUser>("User");
尝试在您的URL和实体集中设置Users
,或将Controller更改为UserController