Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 500(内部服务器错误)使用API webservice_C#_Asp.net Mvc_Web Services_Asp.net Web Api - Fatal编程技术网

C# 500(内部服务器错误)使用API webservice

C# 500(内部服务器错误)使用API webservice,c#,asp.net-mvc,web-services,asp.net-web-api,C#,Asp.net Mvc,Web Services,Asp.net Web Api,我试图从MVC5客户机Web服务类调用webapiPut方法。我可以通过它的id来编辑这本书,但是当我编辑任何东西时,我会得到这个错误 响应状态代码不表示成功:500(内部服务器错误) 我已经搜索了一些导致此错误的信息,但无法找到实际问题。有人能帮我指出我做错了什么吗 API控制器 // PUT: api/Books/5 [ResponseType(typeof(void))] public async Task<IHttpActionResult> Put(int id, Book

我试图从
MVC5
客户机Web服务类调用
webapi
Put方法。我可以通过它的id来编辑这本书,但是当我编辑任何东西时,我会得到这个错误

响应状态代码不表示成功:500(内部服务器错误)

我已经搜索了一些导致此错误的信息,但无法找到实际问题。有人能帮我指出我做错了什么吗

API控制器

// PUT: api/Books/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> Put(int id, BookDetailDTO book)
{
    try{
        var bookEdit = db.Books.FirstOrDefault(b => b.Id == id);

        if(bookEdit == null){
            return BadRequest();
        }
        else{
            bookEdit.Title = book.Title;
            bookEdit.Year = book.Year;
            bookEdit.Price = book.Price;
            bookEdit.Author.Name = book.AuthorName;
            bookEdit.Genre = book.Genre;
            await db.SaveChangesAsync();

            return CreatedAtRoute("DefaultApi", new { id = book.Id }, bookEdit);
        }
    }
    catch{
        if (!BookExists(id)){
            return NotFound();
        }
        else{
            throw;
        }
    }
}
图书
DTO
class

public class BookDetailDTO
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int Year { get; set; }
    public decimal Price { get; set; }
    public string AuthorName { get; set; }
    public string Genre { get; set; }
}   
图书模型

public class Book
{
    public int Id { get; set; }
    [Required]
    public string Title { get; set; }
    public int Year { get; set; }
    public decimal Price { get; set; }
    public string Genre { get; set; }

    // Foreign Key
    public int AuthorId { get; set; }
    //public virtual IEnumerable<Author> AuthorID { get; set; }
    // Navigation property
    public virtual Author Author { get; set; }
}
公共课堂教材
{
公共int Id{get;set;}
[必需]
公共字符串标题{get;set;}
公共整数年{get;set;}
公共十进制价格{get;set;}
公共字符串类型{get;set;}
//外键
公共int AuthorId{get;set;}
//公共虚拟IEnumerable AuthorID{get;set;}
//导航属性
公共虚拟作者{get;set;}
}

您是否使用调试器逐步完成了该方法?您需要调试软件。500表示API中发生了致命且未经处理的错误。您需要特别查看的
内容
原因短语
,以确定为什么不接受您的put,或者干脆让调试器准确地告诉您它不关心哪一行:)。是的,我有。调试器一直运行良好,直到最后一行代码确保成功StatusCode()。@Truecolor,这几乎肯定是您的错误代码
bookEdit.Author.Name=book.AuthorName引发了NullReferenceException。我试图使用我在原始帖子中添加的图书模型,但我在
AuthorID
中遇到了一些问题,一个视图错误告诉我需要一个
IEnumerable
AuthorID。这就是我创建BookDetailDTO的原因。我不知道如何用
AuthorID
Author.Name
解决这个问题。
public class Book
{
    public int Id { get; set; }
    [Required]
    public string Title { get; set; }
    public int Year { get; set; }
    public decimal Price { get; set; }
    public string Genre { get; set; }

    // Foreign Key
    public int AuthorId { get; set; }
    //public virtual IEnumerable<Author> AuthorID { get; set; }
    // Navigation property
    public virtual Author Author { get; set; }
}