简体   繁体   中英

JSON return error with ASP

We are using a ASP app written by an outside vendor. I am tasked with making a small change to the app however I don't know anything about asp or json. Through some research I have put this together. I created a text box on the form and I want to return the client IP address to that text box. I wrote a function then a controller. The code for both is below:

The function

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);
    });
}

The Controller

    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);
    }


}
    }

I am getting these errors on this line of code:

return Json(ipAddress, JsonRequestBehavior.AllowGet);

The error I get is:

The best overloaded method match for System.Web.Http.ApiController.Json(string, Newtonsoft.Json.JsonSerializerSettings) has some invalid arguments. Cannot convert from System.Web.Mvc.JsonRequestBehavior to Newtonsoft.Json.JsonSerializerSettings

If someone could please tell me what they mean and how to fix them I would greatly appreciate it.

Json in ApiController with two parameters has a signature of,

protected internal JsonResult<T> Json<T>(
T content,
JsonSerializerSettings serializerSettings
)

Json in Controller with two parameters has a signature of,

protected internal JsonResult Json(
object data,
JsonRequestBehavior behavior
)

getipaddressController inherited from ApiController , but you used Controller method Json . Use,

return new JsonResult()
{
 Data = ipAddress,
 JsonRequestBehavior = JsonRequestBehavior.AllowGet
};

If you still want the behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM