【问题标题】:Web api internal server error [duplicate]Web api内部服务器错误[重复]
【发布时间】:2017-09-15 12:16:09
【问题描述】:

我正在尝试从我的 html 客户端调用 api。它给了我内部服务器错误,但是当我用邮递员尝试它时它可以工作。

这是我的 api 代码

    [AcceptVerbs("POST")]
    public dynamic Add(string post,string title, string user)
    {
        if (post == null)
            throw new Exception("Post content not added");

        if (title == null)
            throw new Exception("Post title not added");

        var u = UserManager.FindByEmailAsync(user);
        Blog blog = Blog.Create(u.Result.Account.RowKey, post, title).Save();

        return new
        {
            id = blog.Id
        };
    }

我的html客户端是这样的

    var d = {
        post: post,
        title: title,
        user: user
    }

        $.ajax({
            type: 'POST',
            url: apiUrl + 'Blog/Add',
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            data: JSON.stringify(d)
        }).done(function (data) {

            console.log(data);

        }).fail(function (error) {

        });

这是我的路由 api 配置代码

        config.Routes.MapHttpRoute(
            name: "RPCApi",
            routeTemplate: "{controller}/{action}/{id}",
            defaults: new
            {
                id = RouteParameter.Optional
            },
            constraints: new
            {
                subdomain = new SubdomainRouteConstraint("api")
            }
        );

任何人都可以在这里帮助我并解释为什么它与邮递员一起工作而不是与我的 html 客户端一起工作吗?

【问题讨论】:

  • 您收到什么错误?
  • 未找到。 404. 基本上是无法到达api端点。
  • 不要抛出 Exception - 这将导致 500。在这些情况下您应该返回 400 结果。
  • 好的,但这不是问题,因为它是由邮递员工作的。我从客户端调用 api 的方式有问题。
  • apiUrl + 'Blog/Add' 是否生成正确的 URL?

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


【解决方案1】:

这是因为您的 json 代表一个具有 3 个属性的对象。您的控制器不带对象,它需要 3 个字段。使用请求消息发送有效负载时,您必须将其作为对象发送,并且您的 Web api 必须具有可以将请求消息反序列化为的单一模型。更改以下内容将起作用,您的 javascript 将保持不变。

有关此方法为何如此有效以及实现相同目标的其他方法的更多信息,请参阅Angular2 HTTP Post ASP.NET MVC Web API 上的上一个答案 (忽略标题中的客户端框架,答案是 Web API 2 特有的

型号

public class SomethingToPost{
    [Required]
    public string Post{get;set;}
    [Required]
    public string Title{get;set;}
    public string User{get;set;}
}

控制器

[AcceptVerbs("POST")]
public dynamic Add(SomethingToPost postThing)
{
    // validation on ModelState
    // action code
}

【讨论】:

    【解决方案2】:

    这可能是因为返回类型 dynamic。将其更改为 int 考虑到您的 id 类型为 Int32 喜欢

    [AcceptVerbs("POST")]
    public int Add(string post,string title, string user)
    {
        if (post == null)
            throw new Exception("Post content not added");
    
        if (title == null)
            throw new Exception("Post title not added");
    
        var u = UserManager.FindByEmailAsync(user);
        Blog blog = Blog.Create(u.Result.Account.RowKey, post, title).Save();
    
        return blog.Id;
    }
    

    【讨论】:

    • 不,它没有解决问题。问题是它在邮递员中工作。所以我假设我的客户端代码有问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    • 2018-09-03
    • 1970-01-01
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多