0

I am using wcf service where i have to access wcf service method and show the returned json data in the browser itself. Currently i tried string method so json data is showing as json string format which is incorrect. I have gone through this link and tried with my code but not getting any data because of complete data is in one list.

Below is the interface IService code:

[ServiceContract]
    public interface IService1
    {

        [OperationContract]
        List<SuccessStoryApp> GetSuccessStoryJson();
        [OperationContract]
        [WebInvoke(Method = "GET",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "/GetBlogList/")]
        string GetBlogList();
    }

Service1.svc Code:

 public string GetBlogList()
        {
            log.Info("GetBlostList method entered");
            List<SuccessStoryApp> storyList = new List<SuccessStoryApp>();
            string jsonString = string.Empty;
            var jsonData = GetSuccessStoryJson();
            storyList = jsonData.Select(x => new SuccessStoryApp
            {
                Author = x.Author,
                AuthorSlug = x.AuthorSlug,
                Available = x.Available,
                Category = x.Category,
                CountryCode = x.CountryCode,
                Description = x.Description,
                LanguageCode = x.LanguageCode,
                PublishedDate = x.PublishedDate,
                Quote = x.Quote,
                ShortDescription = x.ShortDescription,
                StoryImageAlt = x.StoryImageAlt,
                StoryImageTitle = x.StoryImageTitle,
                StoryImageURL = x.StoryImageURL,
                Tags = x.Tags,
                Text = x.Tags,
                Title = x.Title,
                TitleImageURL = x.TitleImageURL,
                Type = x.Type
            }).ToList();
            if (storyList.Count > 0)
            {
                var json = JsonConvert.SerializeObject(storyList);
                jsonString = json;
            }
            else
            {
                jsonString = "Some error occurred";
            }
            return jsonString;
        }
and below is my web.config service configuration:

<system.serviceModel>
    <services>
      <service behaviorConfiguration="Default" name="BlogService.Service1">
        <endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="BlogService.IService1" bindingConfiguration="WHB" />
       
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:60500/Service1.svc" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp  defaultOutgoingResponseFormat="Json" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
        <behavior name="Default">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="WHB" crossDomainScriptAccessEnabled="true">
          <security mode="None" />
        </binding>
      </webHttpBinding>
    </bindings>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
I am able to debug GetBlogList method and it is returning currently json string. I wanted to return here original json format.
Community
  • 1
  • 1
Vikash
  • 340
  • 1
  • 5
  • 24
  • 1
    What do you mean by original JSON format? Can you show an example of the output you're getting and the output you're expecting? – Tim Jan 13 '17 at 08:36
  • Hi Tim, This is my json string which currently i am getting:"[{\"Age\":35,\"FirstName\":\"Peyton\",\"LastName\":\"Manning\"},{\"Age\":31,\"FirstName\":\"Drew\",\"LastName\":\"Brees\"},{\"Age\":29,\"FirstName\":\"Tony\",\"LastName\":\"Romo\"}]" and this is original json format which i am expecting: [{"Age":35,"FirstName":"Peyton","LastName":"Manning"},{"Age":31,"FirstName":"Drew","LastName":"Brees"},{"Age":29,"FirstName":"Tony","LastName":"Romo"}] – Vikash Jan 13 '17 at 09:35
  • Both strings look the same..... – Tim Jan 13 '17 at 15:39
  • Hi Tim, First json is showing in string format and second is without string. both are completely different. – Vikash Jan 16 '17 at 06:34
  • If you're looking at the first string in the debugger, you can ignore the "\" - the debugger is putting that there. The string itself is correct. – Tim Jan 16 '17 at 10:23
  • No Tim, Currently i am return in string format so the same value is showing with "\" in browser. – Vikash Jan 16 '17 at 11:07

0 Answers0