c# - api Controller 中的多个 HttpPost 方法导致 500 Internal server error

标签 c# asp.net-web-api

我的 api Controller 中有两个 HttpPost 方法,当我访问我的 api 时,我收到 500 内部服务器错误。

但令人惊讶的是,当我删除其中一种 HttpPost 方法时,我能够成功访问我的 API 并检索我想要的信息。

知道可能是什么问题吗?

这是两个方法

    [HttpPost]
    [ActionName("PostUser")]
    public HttpResponseMessage PostUser([FromBody]string id)
    {
        var accessToken = id;
        var client = new FacebookClient(accessToken);
        dynamic result = client.Get("me", new { fields = "name,email" });
        string name = result.name;
        string email = result.email;


        var existingUser = this.Repository.FindByUserIdentity(name);

        if (existingUser == null)
        {
            var newUser = new User
            {
                Username = name,
                Email = email,

            };

            var success = this.Repository.CreateAccount(newUser);

            if (!success)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError);
            }

            //return created status code as we created the user
            return Request.CreateResponse<User>(HttpStatusCode.Created, newUser);
        }

        return Request.CreateResponse(HttpStatusCode.OK);

    }

    [HttpPost]
    [ActionName("PostEmail")]
    public HttpResponseMessage PostEmail([FromBody]string id)
    {
        var accessToken = id;
        var client = new FacebookClient(accessToken);
        dynamic result = client.Get("me", new { fields = "email" });
        string name = result.name;
        string email = result.email;


        var existingUser = this.Repository.FindByUserIdentity(name);

        if (existingUser == null)
        {
            var newUser = new User
            {                   
                Email = email

            };

            var success = this.Repository.CreateAccount(newUser);

            if (!success)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError);
            }

            //return created status code as we created the user
            return Request.CreateResponse<User>(HttpStatusCode.Created, newUser);
        }

        return Request.CreateResponse(HttpStatusCode.OK);

    }

编辑:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Competition", action = "all", id = UrlParameter.Optional }
        );
    }

最佳答案

这是因为您的 Controller 中有两个 Post 方法,并且约定 WebApi 只允许一个 Post 方法。

要配置两个或多个 Post 方法,您必须配置路由设置。

详情请看下面的回答

Multiple HttpPost method in MVC4 Web API Controller

关于c# - api Controller 中的多个 HttpPost 方法导致 500 Internal server error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13046793/

相关文章:

c# - ContinueWith 方法不会等到异步任务完成

c# - 将自定义上下文菜单项添加到 Windows 窗体标题栏

c# - 从字符串映射枚举

c# - Cannon 的矩阵乘法算法

c# - 如何使用 Web API 2 表示成功?

c# - 简单注入(inject)器 Web Api Controller 构造函数注入(inject)失败

c# - 线程等待时持续处理

c# - Convert.ToDateTime(D) 与 (DateTime)D 相比

c# - ASP.net 网络 API 2 错误 "The parameters dictionary contains a null entry for parameter"

asp.net mvc 和 web api 哪个更好 Http POST 或 PUT