我遇到了web api的问题。我在我的LoginsController
中为Web api写了这个动作。
[HttpPost]
public string loginDetail(Login obj)
{
Login email = db.Logins.Find(obj.email);
if (email == null)
{
return null;
}
Login detail = db.Logins.Find(obj.email);
if (detail.password.Equals(obj.password))
{
string type = detail.type;
return type;
}
else
{
return null;
}
}`
这是我的LoginsController
` // GET:api /登录 public IQueryable GetLogins() { return db.Logins; }
// GET: api/Logins/5
[ResponseType(typeof(Login))]
public IHttpActionResult GetLogin(string id)
{
Login login = db.Logins.Find(id);
if (login == null)
{
return NotFound();
}
return Ok(login);
}
[HttpPost]
public string loginDetail(Login obj)
{
Login email = db.Logins.Find(obj.email);
if (email == null)
{
return null;
}
Login detail = db.Logins.Find(obj.email);
if (detail.password.Equals(obj.password))
{
string type = detail.type;
return type;
}
else
{
return null;
}
}
// PUT: api/Logins/5
[ResponseType(typeof(void))]
public IHttpActionResult PutLogin(string id, Login login)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != login.email)
{
return BadRequest();
}
db.Entry(login).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!LoginExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/Logins
[ResponseType(typeof(Login))]
public IHttpActionResult PostLogin(Login login)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Logins.Add(login);
try
{
db.SaveChanges();
}
catch (DbUpdateException)
{
if (LoginExists(login.email))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = login.email }, login);
}
// DELETE: api/Logins/5
[ResponseType(typeof(Login))]
public IHttpActionResult DeleteLogin(string id)
{
Login login = db.Logins.Find(id);
if (login == null)
{
return NotFound();
}
db.Logins.Remove(login);
db.SaveChanges();
return Ok(login);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool LoginExists(string id)
{
return db.Logins.Count(e => e.email == id) > 0;
}
}
}
when i want to send an http post method to "/api/Logins/loginDetail" this error appears:
"找到了与请求匹配的多个操作:类型为Schoolmngt.Controllers.LoginsController的loginDetail PostLogin类型为Schoolmngt.Controllers.LoginsController"`
这是WebApiConfig.cs
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi2",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi3",
routeTemplate: "api/{controller}/{action}",
defaults: new { id = RouteParameter.Optional }
);
我不明白为什么会这样。
答案 0 :(得分:0)
在这种情况下,我建议使用属性路由。例如,您可以像这样命名您的Action方法,
[Route("Login")]
[HttpPost]
public string loginDetail(Login obj)
{
...
}
有关详细信息,请参阅此信息。我认为这将解决您的问题。 here