这是我的第一个Web应用程序项目。我在视图模型中使用的是VS社区,asp.net,引导程序4,C#和JS剔除,服务器端数据来自使用Entity Framework的公司ERP SQL数据库。
这个想法是,用户会收到要从Company ERP系统批准的项目列表,这些项目已加载到视图模型中。视图模型的结构是一个JS敲除的可观察数组,并且每个项目都是一个JS敲除的可观察项(请参见下面的完整代码)
一旦用户根据需要处理了项目,我希望Web应用将整个View Modal作为Json对象发回,并让服务器Post Controller将该Json对象转换为xml并发送到存储的SQL中从SQL数据库插入数据的过程将被处理并插入到ERP数据库中
当我尝试对帖子进行操作时,我收到405“不允许使用方法”
> "tags": {
"ai.cloud.roleInstance": "[MYCOMPUTER].local",
"ai.operation.id": "c07680cd8c845240a9e3791018c39521",
"ai.operation.name": "POST ReqsTests",
"ai.location.ip": "::1",
"ai.internal.sdkVersion": "web:2.8.0-241",
"ai.internal.nodeName": "[MYCOMPUTER].local"
},
"data": {
"baseType": "RequestData",
"baseData": {
"ver": 2,
"id": "|c07680cd8c845240a9e3791018c39521.66f8d951_",
"name": "POST ReqsTests",
"duration": "00:00:00.0279394",
"success": false,
"responseCode": "405",
"url": "http://localhost:64234/api/ReqsTests/",
"properties": {
"DeveloperMode": "true",
"_MS.ProcessedByMetricExtractors": "(Name:'Requests', Ver:'1.1')"
}
}
}
}
我认为我没有从客户端正确收到Json日期。我的想法是因为我要发回整个模型,这是一个Json列表,但是我的控制器没有接收到List而是一个字符串。
谁能解释我的控制器应如何接收客户端数据 这是我从客户端和服务器向我的Controller发出的呼叫Post controller和完整的代码清单在下面
self.postAllReqs = function(self) {
self.error(''); // Clear error message
var data = ko.toJSON(self.Reqs); // convert to json
console.log(data);
ajaxHelper(reqsUri, 'POST', data).fail(function (jqXHR, textStatus, errorThrown) {
self.error(errorThrown);
});
}
// POST: api/ReqsTests
public IHttpActionResult PostReqsTest(string json)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
XmlDocument doc = JsonConvert.DeserializeXmlNode(json);
try
{
//SQL store procedure
SqlParameter param1 = new SqlParameter("@XmlIn", doc);
db.Database.ExecuteSqlCommand("exec [CHC_Web].[TestWebHandShake],@XmlIn",
param1);
}
catch (Exception e)
{
string message = e.Message;
return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, message));
}
return Ok();
}
谢谢
查看模型代码
function ReqsTest(rt) {
rt = rt || {};
var self = this;
self.id = ko.observable(rt.ID || 0);
self.requisition = ko.observable(rt.Requisition || "");
self.reqnStatus = ko.observable(rt.ReqnStatus || "");
self.dateReqnRaised = ko.observable(rt.DateReqnRaised|| null);
self.reqnValue = ko.observable(rt.ReqnValue || null);
self.approvedValue = ko.observable(rt.ApprovedValue || null);
self.originator = ko.observable(rt.Originator || "");
self.origName = ko.observable(rt.OrigName || "");
self.origEmail = ko.observable(rt.OrigEmail || "");
self.line = ko.observable(rt.Line || 0.00);
self.indx = ko.observable(rt.INDX || 0);
self.dateReqnRaisedL = ko.observable(rt.DateReqnRaisedL || null);
self.reqStatus = ko.observable(rt.ReqStatus || "");
//self.reqBackground = ko.observable(rt.ReqBackground || "");
//Computed observables
self.reqBackground = ko.computed(function () {
// get variable
var status = self.reqStatus();
if (status == "A") { return "card-heading bg-success text-white"; }
else if (status == "D") { return "card heading bg-secondary"; }
else if (status == "R") { return "card heading bg-warning"; }
else if (status == "E") { return "card heading bg-danger"; }
else {
return "card-heading bg-primary text-white";
}
})
self.reqStatusLabel = ko.computed(function () {
// get variable
var status = self.reqStatus();
if (status == "A") { return "Approved"; }
else if (status == "D") { return "Declined - put on hold"; }
else if (status == "R") { return "Routing On"; }
else if (status == "E") { return "Erase On Syspro"; }
else {
return "Awaiting Approval";
}
})
self.approvalBtn = ko.computed(function () {
// get variable
var status = self.reqStatus();
if (status == "A") { return "css: button btn-secondary "; }
else {
return "btn btn-success ";
}
})
self.approvalBtnLbl = ko.computed(function () {
// get variable
var status = self.reqStatus();
if (status == "W") { return "Approve"; }
else {
return "UnApprove";
}
})
self.declineBtnLbl = ko.computed(function () {
// get variable
var status = self.reqStatus();
if (status == "D") { return "UnDecline"; }
else {
return "Decline";
}
})
self.deleteBtnLbl = ko.computed(function () {
// get variable
var status = self.reqStatus();
if (status == "E") { return "Restore"; }
else {
return "Erase";
}
})
// Functions
//show details alert
$(".btn").on("click", function () {
$(".alert").removeClass("in").show();
$(".alert").delay(200).addClass("in").fadeOut(2000);
});
}
function ReqsViewModel (){
var self = this;
self.Reqs = ko.observableArray([]);
self.error = ko.observable();
var reqsUri = '/api/ReqsTests/';
function ajaxHelper(uri, method, data) {
self.error(''); // Clear error message
return $.ajax({
type: method,
url: uri,
dataType: 'json',
contentType: 'application/json',
data: data ? JSON.stringify(data) : null
}).fail(function (jqXHR, textStatus, errorThrown) {
self.error(errorThrown);
});
}
function getAllReqs() {
ajaxHelper(reqsUri, 'GET').done(function (data) {
// Build the ReqsTest objects
var reqs = ko.utils.arrayMap(data, function (rt) {
return new ReqsTest(rt);
});
self.Reqs(reqs);
});
}
self.postAllReqs = function(self) {
self.error(''); // Clear error message
var data = ko.toJSON(self.Reqs); // convert to json
console.log(data);
ajaxHelper(reqsUri, 'POST', data).fail(function (jqXHR, textStatus, errorThrown) {
self.error(errorThrown);
});
}
// Details
self.detail = ko.observable();
self.getReqDetail = function (item) {
//var url = reqsUri + item.indx();
//ajaxHelper(url, 'GET').done(function (data) {
// self.detail(data);
//}
//);
self.detail(item)
}
//Approval function
self.Approval = function (item) {
var status = item.reqStatus();
if (status == "W") { item.reqStatus("A"); }
else
{ item.reqStatus("W"); }
self.getReqDetail(item);
}
//Decline function
self.Decline = function (item) {
var status = item.reqStatus();
if (status == "D") { item.reqStatus("W"); }
else { item.reqStatus("D"); }
self.getReqDetail(item);
}
//Delete function
self.Delete = function (item) {
var status = item.reqStatus();
if (status == "E") { item.reqStatus("W"); }
else { item.reqStatus("E"); }
self.getReqDetail(item);
}
// Load the reqs - Take this out if you don't want it
getAllReqs();
}
ko.applyBindings(new ReqsViewModel());
模型类
namespace POC_Reqs_v1.Models
{
using System;
using System.Collections.Generic;
public partial class ReqsTest
{
public string ID { get; set; }
public string Requisition { get; set; }
public string ReqnStatus { get; set; }
public Nullable<System.DateTime> DateReqnRaised { get; set; }
public Nullable<decimal> ReqnValue { get; set; }
public Nullable<decimal> ApprovedValue { get; set; }
public string Originator { get; set; }
public string OrigName { get; set; }
public string OrigEmail { get; set; }
public decimal Line { get; set; }
public long INDX { get; set; }
public string DateReqnRaisedL { get; set; }
public string ReqStatus { get; set; }
public string ReqBackground { get; set; }
}
}
控制器代码
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using System.Xml;
using Newtonsoft.Json;
using POC_Reqs_v1.Models;
namespace POC_Reqs_v1.Controllers
{
public class ReqsTestsController : ApiController
{
private ChamberlinWebEntities db = new ChamberlinWebEntities();
// GET: api/ReqsTests
public IQueryable<ReqsTest> GetReqsTests()
{
return db.ReqsTests;
}
// GET: api/ReqsTests/5
[ResponseType(typeof(ReqsTest))]
public async Task<IHttpActionResult> GetReqsTest(string id)
{
var ID = Convert.ToInt64(id);
ReqsTest reqsTest = await db.ReqsTests.FindAsync(ID);
if (reqsTest == null)
{
return NotFound();
}
return Ok(reqsTest);
}
// PUT: api/ReqsTests/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutReqsTest(string id, ReqsTest reqsTest)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != reqsTest.ID)
{
return BadRequest();
}
db.Entry(reqsTest).State = EntityState.Modified;
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ReqsTestExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/ReqsTests
public IHttpActionResult PostReqsTest(string json)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
XmlDocument doc = JsonConvert.DeserializeXmlNode(json);
try
{
//SQL store procedure
SqlParameter param1 = new SqlParameter("@XmlIn", doc);
db.Database.ExecuteSqlCommand("exec [CHC_Web].[TestWebHandShake],@XmlIn",
param1);
}
catch (Exception e)
{
string message = e.Message;
return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, message));
}
return Ok();
}
// DELETE: api/ReqsTests/5
[ResponseType(typeof(ReqsTest))]
public async Task<IHttpActionResult> DeleteReqsTest(string id)
{
ReqsTest reqsTest = await db.ReqsTests.FindAsync(id);
if (reqsTest == null)
{
return NotFound();
}
db.ReqsTests.Remove(reqsTest);
await db.SaveChangesAsync();
return Ok(reqsTest);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool ReqsTestExists(string id)
{
return db.ReqsTests.Count(e => e.ID == id) > 0;
}
}
}
答案 0 :(得分:3)
为完整起见,这是我的最终解决方案: 主要问题是post控制器中的语法错误,编译时没有产生错误,错误的行是
// POST: api/ReqsTests
public IHttpActionResult PostReqsTest(string json)
正确的语法在最后
public async Task<IHttpActionResult> PostReqsTest(object json)
所以完整的控制器代码是
// POST: api/ReqsTests
public async Task<IHttpActionResult> PostReqsTest(object json)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
//convert the Json model to xml
XmlDocument doc = JsonConvert.DeserializeXmlNode(json.ToString());
try
{
//SQL store procedure
SqlParameter param1 = new SqlParameter("@XmlIn", doc.InnerXml);
db.Database.ExecuteSqlCommand("exec [CHC_Web].[TestWebHandShake] @XmlIn",
param1);
}
catch (Exception e)
{
string message = e.Message;
return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, message));
}
return ResponseMessage(Request.CreateResponse HttpStatusCode.OK,"Inserted to database")); }