asp.net-mvc-4 - Web API 操作名称

标签 asp.net-mvc-4 asp.net-web-api

我正在使用 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/

相关文章:

javascript - jQuery Ajax 无法调用 MVC 4 Controller 方法

c# - Api Controller 声明多个 Get 语句

asp.net-web-api - Breezejs,跨域

c# - ASP.net web api 2 路由属性不工作

c# - 包含循环,如果引用跟踪被禁用,则无法序列化,json.net 和 webapi

json - 如何从 MVC Controller 操作方法调用 Web Api 方法?

asp.net-mvc - 打开子表单时应该如何保存和重新填充表单数据?

asp.net-mvc - 如何在 ASP.NET MVC API Controller 中获取用户 IP

asp.net-web-api - HttpSelfHostConfiguration虽然引用了system.web.http但是无法解析

c# - 使用 HttpResponseMessage 进行长时间操作的异步