我正在使用 ASP.NET Web API,并且有以下内容:
public Guid GetLogon(string username, string password)
{
return new X.Authentication().Logon(username, password);
}
public void PostLogoff(Guid sessionId)
{
new X.Authentication().Logoff(sessionId);
}
这是从客户端调用的,如下所示:
$(document).ready(function () {
logon("MyUsername", "MyPassword", function (guid) {
$("#sessionId").html(guid);
logoff($("#sessionId").html(), function () {
//Logged out action here
});
});
});
这一切都有效,但我不喜欢在操作名称前添加 http 动词,例如 GetLogon 或 PostLogoff。有没有办法让他们只是登录和注销?
我尝试了以下方法,但没有成功:
[System.Web.Mvc.AcceptVerbs(HttpVerbs.Get)]
public Guid Logon(string username, string password)
{
return new X.Authentication().Logon(username, password);
}
[System.Web.Mvc.AcceptVerbs(HttpVerbs.Post)]
public void Logoff(Guid sessionId)
{
new X.Authentication().Logoff(sessionId);
}
提前谢谢
最佳答案
不确定这是否是原因,但在我的 APIController 中,AcceptVerbs 属性位于不同的命名空间中:
System.Web.Http.AcceptVerbs
[HttpGet]
和 [HttpPost]
属性位于同一命名空间
我在同一个项目中有多个常规 MVC Controller ;他们使用您上面描述的命名空间,但不使用 API Controller
试试这个:
[System.Web.Http.AcceptVerbs("GET")]
public Guid Logon(string username, string password)
{
return new X.Authentication().Logon(username, password);
}
[System.Web.Http.AcceptVerbs("POST")]
public void Logoff(Guid sessionId)
{
new X.Authentication().Logoff(sessionId);
}
关于asp.net-mvc-4 - Web API 操作名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10519488/