Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 775
mulefeathers Avatar asked Mar 29 '16 13:03

mulefeathers


People also ask

Can we return JSON through Viewresult?

@Zach Yes, It's possible. You can return your html with model. Create a partial view and return the partial view with model instead of json result.

What is JsonRequestBehavior?

JsonRequestBehavior. A value that indicates whether HTTP GET requests from the client are allowed.

What is JSON in ASP NET MVC?

Json(Object, String, Encoding, JsonRequestBehavior) Creates a JsonResult object that serializes the specified object to JavaScript Object Notation (JSON) format using the content type, content encoding, and the JSON request behavior.


1 Answers

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.

like image 136
Mike Debela Avatar answered Oct 20 '22 01:10

Mike Debela