1
string apiUrl1 = string.Format(@"http://xxx.xxx.x.xxx/test/Home/getdata?id=1");

public static string GetData(string url)
    {

        string Result = "";
        try
        {
            using (WebClient client = new WebClient())
            {
                Result = client.DownloadString(string.Format(@"" + url + ""));


            }
        }
        catch (Exception e)
        {
            string msg = e.Message;
            Result = "";
        }
        return Result;
    }

I am trying to get Data from my Published .Net MVC Web Project But i am getting Server Error "The remote server returned an error: (500) Internal Server Error."

My MVC Controller Code is Here

public JsonResult getdata(int? id)
    {
        List<Items> dbItems = Items.getItemsData(id);

        return Json(dbItems, JsonRequestBehavior.AllowGet);
    }

1 Answer 1

1

Besides the fact that you would probably be better suited using ASP.NET Web API (you wouldn't have to specifically convert your response to JSON on your controller), you can try something like below. As far as the server error, you would have to debug that server side.

  public class RestClient
  {  
    HttpClient client;
    private string RestUrl = "http://192.168.1.103/test/Home/";
    private static List<Items> _items = new List<Items>();

    public RestClient()
    {
        client = new HttpClient();
        client.MaxResponseContentBufferSize = 9999999;
    }

    public async Task<List<Item>> GetItems(int? id)
    {            
         List<Item> items= new List<Item>();
         var uri = new Uri(RestUrl + "getdata?id=" + id);
         var response = await client.GetAsync(uri);
         if (response.IsSuccessStatusCode)
         {
            var content = await response.Content.ReadAsStringAsync();
            items= JsonConvert.DeserializeObject<List<Item>>(content);
            _items.AddRange(items);
         }
         else
         {
            //do something
         }    

        return _items;
    }
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.