Solved

Check if HttpResponseMessage = HttpStatusCode.NotFound

Posted on 2013-05-31
1
695 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 74

Accepted Solution

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

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

Open in new window

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now