WebAPI,我收到了HTTP 500错误。我输入的警报正在正确触发。但是我在ProductsController.cs的Post方法中放了一个断点。它并没有停在断点处,似乎表明路由不对。我的目的是让Post方法将新行添加到数组中,并在列表中显示新行。你能帮我吗?以下是代码的当前状态:
++++++++++++++++++++++++++++++++++ Default.aspx +++++++++++++++++++++++++++++++++
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebAPISPA6.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Default</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"/>
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
getProducts();
$("#btn").on('click', function () {
var prodDTO = {}
alert('on click');
prodDTO.Id = $('#<%=txtId.ClientID%>').val();
prodDTO.Name = $('#<%=txtName.ClientID%>').val();
prodDTO.Category = $('#<%=txtCategory.ClientID%>').val();
prodDTO.Price = $('#<%=txtPrice.ClientID%>').val();
$.ajax({
url: 'http://localhost:65472/api/products',
type: 'POST',
//4.
data: prodDTO,
ContentType: 'application/json;utf-8',
datatype: 'json'
}).done(function (resp) {
console.log(resp);
}).error(function (err) {
console.log("Error " + err.status);
});
alert("Post on click");
return false;
});
var products; //global variable products
function getProducts() {
$.getJSON("http://localhost:65472/api/products",
function (data) {
$('#products').empty(); // Clear the table body.
//Assign the return value to products variable
products = data;
// Loop through the list of products.
$.each(data, function (key, val) {
// Add a table row for the product.
var row = '<td>' + val.Name + '</td><td>' + val.Price + '</td>';
$('<tr/>', { html: row }) // Append the name.
.appendTo($('#products'));
});
alert('exiting getproducts');
});
};
//function appendItem() {
// var obj = products;
// obj.push({ "Id": "4", "Name": "Goya Guava Drink", "Category": "Beverages", "Price": "1.50" });
// //if you want to display the change in table then you need to repopulate the table here
// $('#products').empty();
// // Loop through the list of products.
// $.each(obj, function (key, val) {
// // Add a table row for the product.
// var row = '<td>' + val.Name + '</td><td>' + val.Price + '</td>';
// $('<tr/>', { html: row }) // Append the name.
// .appendTo($('#products'));
// });
// return false;
//};
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Products</h2>
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody id="products">
</tbody>
</table>
<hr />
Id:
<asp:TextBox runat="server" ID="txtId">10</asp:TextBox>
Name:
<asp:TextBox runat="server" ID="txtName">Test</asp:TextBox>
Category:
<asp:TextBox runat="server" ID="txtCategory">Test Category</asp:TextBox>
Price:
<asp:TextBox runat="server" ID="txtPrice">5.50</asp:TextBox>
</div>
<p>
<input id="btn" type="button" value="POST" />
</p>
</form>
</body>
</html>
+++++++++++++++++++++++++++++++++++++++++++++ ProductsController.cs ++++++++++++++++++++++++++++
namespace WebForms
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
public class ProductsController : ApiController
{
Product[] products = new Product[]
{
new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
};
public IEnumerable<Product> GetAllProducts()
{
return products;
}
public Product GetProductById(int id)
{
var product = products.FirstOrDefault((p) => p.Id == id);
if (product == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return product;
}
public IEnumerable<Product> GetProductsByCategory(string category)
{
return products.Where(
(p) => string.Equals(p.Category, category,
StringComparison.OrdinalIgnoreCase));
}
public string AppendRow([FromBody] YourObjectDto prodDTO)
{
var id = prodDTO.Id;
var name = prodDTO.Name;
var category = prodDTO.Category;
var price = prodDTO.Price;
//new Product { Id = id, Name = name, Category = category, Price = price };
return "success";
}
// POST api/Products
public Product Post([FromBody]Product prodDTO)
{
return prodDTO;
}
}
}