Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Web API - Internal Server Error 500

When I have a return type of 'string' in my WebAPI controller, the SuccessStatusCode returns 'OK' in my MVC Controller, but when the return type is of a model named 'USER', I get this Internal Server Error. Here's my code:

WebAPI:

public class UserController : ApiController
{
    OnlineCenterEntities db = new OnlineCenterEntities();

    public USER GetUserInfo(string userName, string domain)
    {
        USER userInfo = (from u in db.USERs
                         where u.USER_NAME.ToUpper() == userName.ToUpper() && u.LDAP_NAME.ToUpper() == domain.ToUpper()
                         select u).FirstOrDefault();

        return userInfo;
    }
}

MVC Controller that calls the WebAPI:

 public class HomeController : Controller
{
    HttpClient client;
    string url = "http://localhost:61566/api/user/";

    public HomeController()
    {
        client = new HttpClient();
        client.BaseAddress = new Uri(url);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    }

    public async Task<ActionResult> Index(string userName, string domain)
    {
        string GetUserInfoURL = String.Format("GetUserInfo?userName={0}&domain={1}", userName, domain);
        HttpResponseMessage responseMessage = await client.GetAsync(url+GetUserInfoURL);

        if (responseMessage.IsSuccessStatusCode)
        {
            var responseData = responseMessage.Content.ReadAsStringAsync().Result;
            var userInfor = JsonConvert.DeserializeObject<USER>(responseData);
        }

        return View();
    }

USER model:

public partial class USER
{

    public int USER_ID { get; set; }
    public string USER_NAME { get; set; }
    public string FIRST_NAME { get; set; }
    public string LAST_NAME { get; set; }
    public string LDAP_NAME { get; set; }
    public string EMAIL { get; set; }
}

In my WebAPI, if I change the return type from USER to string (and of course, change the return variable type to some string (userInfo.FIRST_NAME)), I get the SuccessStatusCode as 'OK', but as of this code, I get Internal Server Error with StatusCode: 500 (whatever that means). I have tried inserting breakpoint at every possible points, and I know that the api is returning the result fine. I simply don't understand why the following line

HttpResponseMessage responseMessage = await client.GetAsync(url+GetUserInfoURL);

gives InternalServerError error when I have the return type of USER, and return the whole USER model instead of just one string.

Please don't worry about the userName and domain parameters that I'm passing to the controllers, they are working fine!

like image 318
Bivo Kasaju Avatar asked Jul 06 '16 19:07

Bivo Kasaju


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Typically when this happens, it means it is failing to serialize the response. Once your controller returns a USER instance, somewhere WebAPI has to serialize that into the format requested by the client.

In this case the client requested "application/json". The default JsonMediaTypeFormatter uses JSON.Net to turn your C# object into json for the client. Apparently that serialization step is failing, but the response code doesn't tell you exactly why.

The easiest way to see exactly what is happening is to use a custom MessageHandler which forces the body to buffer earlier so you can see the actual exception. Take a look at this blog post for an example to force it to show you the real failure.

like image 197
Tim Copenhaver Avatar answered Sep 24 '22 08:09

Tim Copenhaver