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 8112465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:30:34+00:00 2026-06-06T02:30:34+00:00

Below is my Get method of MVC Web Api RC. public Employee Get(int id)

  • 0

Below is my Get method of MVC Web Api RC.

public Employee Get(int id)
{
     Employee emp= null;

     //try getting the Employee with given id, if not found, gracefully return error message with notfound status
     if (!_repository.TryGet(id, out emp))
         throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)
         {
             Content = new StringContent("Sorry! no Employee found with id " + id),
             ReasonPhrase = "Error"
         });

      return emp;
}

Here problem is that whenever an error is thrown “Sorry! no Employee found with id “, is just in a plane text format. However i wanted to set the format as per my current formatter. Like by default i have set XML formatter in my global.asax. So the error should be displayed in XML format. Something like :

<error>
  <error>Sorry! no Employee found with id </error>
</error>

similarly for Json formatter. It should be :

[{"errror","Sorry! no Employee found with id"}]

Thanks in advance

  • 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-06T02:30:35+00:00Added an answer on June 6, 2026 at 2:30 am

    You are returning a StringContent. This means that the contents will be returned as-is and it is up to you to format it.

    Personally I would define a model:

    public class Error
    {
        public string Message { get; set; }
    }
    

    and then:

    if (!_repository.TryGet(id, out emp))
    {
        var response = Request.CreateResponse(
            HttpStatusCode.NotFound,
            new Error { Message = "Sorry! no Employee found with id " + id }
        );
        throw new HttpResponseException(response);
    }
    

    A XML Accept enabled client would then see:

    <Error xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/AppName.Models">
        <Message>Sorry! no Employee found with id 78</Message>
    </Error>
    

    and a JSON Accept enabled client would see:

    {"Message":"Sorry! no Employee found with id 78"}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to send GET Request method with the below headers . I am
I have problem with android 4.0.3. I'm using the method below to get local
I have the method below, which in my Blackjack app will get the value
I am developing an ASP.Net MVC 3 Web application and believe my method of
I'm learning and try to get a firm grasp of correctly implementing sound MVC
I get an error in the bellow code it says I cannot convert method
The two programs below get n integers from file and calculates the sum of
I use the line below to get thumbnail: bitmap = MediaStore.Images.Thumbnails.getThumbnail(act.getApplicationContext().getContentResolver(), fid, MediaStore.Images.Thumbnails.MICRO_KIND, null);
I am using the script code below to get results from my database based
I'm trying to do a preg match all on the response below to get

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.