【问题标题】:More than one HttpPost methods in a api controller result in 500 Internal server errorapi 控制器中的多个 HttpPost 方法导致 500 Internal server error
【发布时间】:2012-10-24 09:53:15
【问题描述】:

我的 api 控制器中有两个 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 }
        );
    }

【问题讨论】:

  • 能发一下路由配置吗?
  • @CuongLe 发布了路由配置

标签: c# asp.net-web-api


【解决方案1】:

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

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

详细看下面的回答

Multiple HttpPost method in MVC4 Web API Controller

【讨论】:

  • 非常感谢!! :D 我整天都在胡思乱想!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-18
  • 2020-02-01
  • 2017-09-06
  • 2017-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多