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

  • Home
  • SEARCH
  • 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 8196151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:23:12+00:00 2026-06-07T05:23:12+00:00

I created a web service (REST) in C#. Now I want that when someone

  • 0

I created a web service (REST) in C#. Now I want that when someone uses it, it should return JSON or XML as per Header. I found a very good tutorial here. I followed it but I dont know where it says set both the HTTP Accept and Content-Type headers to "application/xml", I am calling it in this way http://localhost:38477/social/name. I can answer to any question if my question is not very clear to you
Thanks
THis is my code

[WebInvoke(UriTemplate = "{Name}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
        public MyclassData Get(string Name)
        {
            // Code to implement
            return value;

        }
  • 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-07T05:23:15+00:00Added an answer on June 7, 2026 at 5:23 am

    What framework are you using (Looks like the older WCf Web Api) to build your RESTful service? I would highly recommend using Microsofts new MVC4 Web API. It is really starting to mature and greatly simplifies building RESTful services. It is what is going to be supported in the future where the WCF Web API is about to be discontinued.

    You simply return your ModelClass as a return type and it will automatically serialize it into XML or JSON depending on the requests accept header. You avoid having writing duplicate code and your service will support a wide range of clients.

    public class TwitterController : ApiController
    {
         DataScrapperApi api = new DataScrapperApi();
         TwitterAndKloutData data = api.GetTwitterAndKloutData(screenName);
         return data;
    }
    
    public class TwitterAndKloutData
    {
       // implement properties here
    }
    

    Links

    You can get MVC4 Web Api by downloading just MVC4 2012 RC or you can download the whole Visual Studio 2012 RC.

    MVC 4: http://www.asp.net/mvc/mvc4

    VS 2012: http://www.microsoft.com/visualstudio/11/en-us/downloads


    For the original wcf web api give this a shot. Examine the accept header and generate your response according to its value.

    var context = WebOperationContext.Current
    string accept = context.IncomingRequest.Accept;
    System.ServiceModel.Chanells.Message message = null;
    
    if (accept == "application/json")
       message = context.CreateJsonResponse<TwitterAndCloutData>(data);
    else if (accept == "text/xml")
       message = context.CreateXmlResponse<TwitterAndCloutData>(data);
    
    return message;
    

    You would set the accept header on whatever client is initiated the request. This will differ depending on what type of client you are using to send the request but any http client will have a way to add headers.

    WebClient client = new WebClient();
    client.Headers.Add("accept", "text/xml");
    client.DownloadString("domain.com/service");
    

    To access the response headers you would use

    WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
    

    Additional resources: http://dotnet.dzone.com/articles/wcf-rest-xml-json-or-both

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

Sidebar

Related Questions

I have created Web-Service in asp.net with c# . Now i want to pass
I've created a web service that uses a generic type Response<TCode, TData> and so
I'm working with a REST web service that uses basic authentication and returns me
I have created a REST web service that, on Application_Start, subscribes to another service
i have created a Java Restful Web service . Now i want to access
I have created a REST web service and successfully used it with a client.
I ve created a sample REST web service which writes some data in a
I'm trying to create a REST web service that exposes the following Django model:
I have created a simple Northwind's Product REST Web Service in WCF at /Northwind/Product
In my Web service I use eclipse, java 1.5, spring. now I want to

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.