Solved

Check if HttpResponseMessage = HttpStatusCode.NotFound

Posted on 2013-05-31
1
711 Views
Last Modified: 2013-06-03
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

0
Comment
Question by:webressurs
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39210538
Check the StatusCode property:

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

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

766 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question