Check if HttpResponseMessage = HttpStatusCode.NotFound

Hi!

In my ASP.NET Web API (MVC4) I need to check what a method called UserExists() returns. If it is "HttpStatusCode.NotFound" I need to do something, if not I will do something else. Please see code below. How can this be easily done?

protected HttpResponseMessage UserExists(string userName)
{
    var mp = Membership.Providers[API.CurrentSite.ApplicationName];
    var user = mp.GetUser(userName.Trim(), false);

    if (user != null)
    {
	return Request.CreateResponse(HttpStatusCode.Found);
    }
    return Request.CreateResponse(HttpStatusCode.NotFound);
}

public HttpResponseMessage PostUser(API.Models.UserData user)
{
    if (UserExists(user.UserName) == HttpStatusCode.NotFound) //THIS CHECK DON'T WORK 
    {
        // Do something
    }
    else
    {
        // Do something else
    }
{

Open in new window

LVL 1
webressursAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
käµfm³d 👽Connect With a Mentor Commented:
Check the StatusCode property:

if (UserExists(user.UserName).StatusCode == HttpStatusCode.NotFound)

Open in new window

0
All Courses

From novice to tech pro — start learning today.