Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7976853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:01:21+00:00 2026-06-04T09:01:21+00:00

I tried to understand both and write sample code: public HttpResponseMessage Get() { var

  • 0

I tried to understand both and write sample code:

 public HttpResponseMessage Get()
 {
     var response = ControllerContext.Request
                         .CreateResponse(HttpStatusCode.BadRequest, "abc");

     throw new HttpResponseException(response);
 }

And:

 public HttpResponseMessage Get()
 {
     return ControllerContext.Request
                        .CreateResponse(HttpStatusCode.BadRequest, "abc");
 }

From Fiddle, I really didn’t see any differences between them, so what is the purpose of using HttpResponseException?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:01:23+00:00Added an answer on June 4, 2026 at 9:01 am

    The main difference between the two is this. The exception is useful to immediately stop processing and exit. For example assume I have the following code

    public class CustomerController : ApiController {
      private ICustomerContext repo;
    
      public CustomerController(ICustomerContext repo) {
        this.repo = repo;
      }
    
      public Customer Get(int id) {
        var customer = repo.Customers.SingleOrDefault(c=>c.CustomerID == id);
        if (customer == null) {
          throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
        }
        return customer;
      }
    }
    

    If this code runs and I pass an id that is not present, it will immediately stop processing and return a status code of 404.

    If instead I return HttpResponseMessage, the request will happily continue the rest of its processing and return a 404. The main difference being end the request or not.

    As Darrel said the exception is useful in cases where in some cases I want processing to continue (as in when customer is found) and in others I don’t.

    The place where you might want to use something like HttpResponseMessage is in an Http POST to return a status code of 201 and set the location header. In that case I do want processing to continue. That would would do with this code.*

    public class CustomerController : ApiController {
      private ICustomerContext repo;
    
      public CustomerController(ICustomerContext repo) {
        this.repo = repo;
      }
    
      public HttpResponseMessage Post(Customer customer) {
        repo.Add(customer);
        repo.SaveChanges();
        var response = Request.CreateResponse(HttpStatusCode.Created, customer);
        response.Headers.Location = new Uri(Request.RequestUri, string.format("customer/{0}", customer.id));
        return response;
      }
    }
    

    *note: If you are using the beta bits you would create a new HttpResponseMessage. I am using the later bits however which require you to use the CreateResponse extension method off of the Request.

    Above, I am creating a response which sets the status code to 201, passes in the customer, and then sets the location header.

    The response is then returned and the request continues processing.

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to understand this line of code, but it is failed. $this->request->{self::FLAG_SHOW_CONFIG} ===
I tried to write a program that uses threads, but couldn't understand the o/p.
I've tried to understand some of the posts of similar, but don't quite understand
I have tried to understand AOP, Dependency Injection and Inversion of Control SPRING related
Tried examples from 'php.net' but don't understand what's the problem. Any suggestions? <?php $_SESSION['test']
I understand that Aptana is built from Eclipse. I tried searching for the Package
I just tried dynamodb example from amazon. I understand the advantages on the db
I read lot's of docs, tried everything and still can't understand why my template
In tutorials I've learnt to use document.write . Now I understand that by many
I've been looking to the OpenCV's magnificent source code to understand the score calculation.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.