我在这个项目上遇到了一些麻烦。我是WCF和Web服务的新手,我需要通过POST接收数据,并且我得到了一个具有POST功能的小项目。
IBookService.cs
namespace SimpleRESTServiceCRUD
{
[ServiceContract]
public interface IBookService
{
[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "Books/")]
List<Book> GetBookList();
[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "Book/{id}")]
Book GetBookById(string id);
[OperationContract]
[WebInvoke(Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "AddBook/{id}")]
string AddBook(Book book, string id);
[OperationContract]
[WebInvoke(Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "UpdateBook/{id}")]
string UpdateBook(Book book, string id);
[OperationContract]
[WebInvoke(Method = "DELETE", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "DeleteBook/{id}")]
string DeleteBook(string id);
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat= WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "SaveBook")]
string SaveBook(String book);
}
}
BookService.svc.cs
namespace SimpleRESTServiceCRUD
{
public class BookService : IBookService
{
static IBookRepository repository = new BookRepository();
public List<Book> GetBookList()
{
return repository.GetAllBooks();
}
public Book GetBookById(string id)
{
return repository.GetBookById(int.Parse(id));
}
public string AddBook(Book book, string id)
{
Book newBook = repository.AddNewBook(book);
return "id=" + newBook.BookId;
}
public string UpdateBook(Book book, string id)
{
bool updated = repository.UpdateABook(book);
if (updated)
return "Book with id = " + id + " updated successfully";
else
return "Unable to update book with id = " + id;
}
public string DeleteBook(string id)
{
bool deleted = repository.DeleteABook(int.Parse(id));
if (deleted)
return "Book with id = " + id + " deleted successfully.";
else
return "Unable to delete book with id = " + id;
}
public string SaveBook(String book)
{
var a = book;
// newBook = repository.AddNewBook(book);
return "id=" + book.ToString();
}
}
}
我一直在尝试使用名为Postman - REST Client的Chrome扩展程序,var book总是返回NULL。它永远不会返回我通过Postman发送的价值。
我一直在寻找各处,我找不到适合我的解决方案。
正如我所说,我对这个主题缺乏了解,所以如果有人可以帮我解决这个问题,或者可以帮助我更简单地解释一下带有POST的WCF,我会很高兴。