我们正在使用由外部供应商编写的 ASP 应用程序。我的任务是对应用程序进行一些小的更改,但是我对 asp 或 json 一无所知。通过一些研究,我将其放在一起。我在表单上创建了一个文本框,我想将客户端 IP 地址返回到该文本框。我写了一个函数,然后写了一个控制器。两者的代码如下:
功能
function processgetip(event) {
// Within this function, make an AJAX call to get the IP Address
$.getJSON('@Url.Action("GetIPAddress","getipaddress")', function (ip) {
// When this call is done, your IP should be stored in 'ip', so
// You can use it how you would like
// Example: Setting a TextBox with ID "YourElement" to your returned IP Address
$("#facility").val(ip);
});
}
控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web;
using System.Web.Mvc;
namespace Parker_Hannifin.Controllers
{
public class getipaddressController : ApiController
{
public JsonResult GetIPAddress()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
//return addresses[0]; //
ipAddress = addresses[0];
}
}
//replace ipaddress with ipAddress
return Json(ipAddress, JsonRequestBehavior.AllowGet);
}
}
}
我在这行代码中收到这些错误:
return Json(ipAddress, JsonRequestBehavior.AllowGet);
我得到的错误是:
最佳重载方法匹配
System.Web.Http.ApiController.Json(字符串,
Newtonsoft.Json.JsonSerializerSettings) 有一些无效参数。
无法从 System.Web.Mvc.JsonRequestBehavior 转换为
Newtonsoft.Json.JsonSerializerSettings
如果有人可以告诉我他们的意思以及如何解决它们,我将不胜感激。