Solved

Check if HttpResponseMessage = HttpStatusCode.NotFound

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

930 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

12 Experts available now in Live!

Get 1:1 Help Now