0
我有一個控制器。代碼如下:與此控制器一起使用的javavascript也在下面。此代碼是使用MVC 5在Visual Studio 2013中創建的。該代碼已發佈到IIS,並按預期工作了幾周。由於需求的變化,代碼被移植到了一個使用Katana的項目中,現在使用控制檯應用程序進行自我清理。控制器和JavaScript是一樣的。問題是,當我打電話添加一個位置時,調用Get方法,但位置變量現在爲空。爲什麼此代碼工作在Internet Information Server中託管,而不是作爲使用Katana的自託管應用程序?Javascript到ASP.NET mvc控制器使用卡塔納失敗
控制器:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;
using PatientCommunication.Model;
using PatientCommunication.Repository;
using PatientCommunication.DataAccess;
using Walden.Utility;
namespace PatientCommunication.Web.Controllers
{
public class LocationController : ApiController
{
// GET api/location
private ILocations locations = new Locations();
public IEnumerable<Location> Get()
{
Database.ReminderConnection = ConfigurationManager.AppSettings["reminderConnection"];
List<Location> locationList = new List<Location>();
locationList = locations.GetLocationList();
return locationList;
}
public void Post([FromBody] Location locationList)
{
Database.ReminderConnection = ConfigurationManager.AppSettings["reminderConnection"];
locations.UpdateLocation(locationList);
}
public ActionResult Put(IEnumerable<Location> location)
{
Database.ReminderConnection = ConfigurationManager.AppSettings["reminderConnection"];
string toBeVoiced = string.Empty;
try
{
foreach (var loc in location)
{
Location singlelocation = new Location();
singlelocation.LocationID = loc.LocationID;
singlelocation.LocationName = loc.LocationName;
locations.AddLocation(singlelocation);
}
return new JsonResult { Data = new { Success = true } };
}
catch (Exception er)
{
string s1 = er.Message;
return new JsonResult { Data = new { Success = false } };
}
}
public void Delete([FromBody] Location locationList)
{
Database.ReminderConnection = ConfigurationManager.AppSettings["reminderConnection"];
locations.DeleteLocation(locationList);
}
}
}
的Javascript:
$('#addLocation').click(function (e) {
if ($('#locationID').val().length < 1) {
ShowAlert("No Text", "Please Input Location ID")
e.preventDefault();
return;
}
if ($('#locationName').val().length < 1) {
ShowAlert("No Text", "Please Input Location Name")
e.preventDefault();
return;
}
locations.push({
ID: 0,
LocationID: $('#locationID').val(),
LocationName: $('#locationName').val()
});
$.ajax({
url: "api/location",
type: "PUT",
datatype: "json",
data: $.toJSON(locations),
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (data.Data.Success) {
ShowAlert("Insert", "Record Inserted");
$("#gridLocations").data("kendoGrid").dataSource.read();
$('#locationID').val('');
$('#locationName').val('');
locations = [];
}
else {
alert("Error'");
}
}
});
e.preventDefault();
});