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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:54:35+00:00 2026-06-18T07:54:35+00:00

I want to add a public (externally callable) JSON data feed to my ASP.net

  • 0

I want to add a public (externally callable) JSON data feed to my ASP.net (4) Forms web site. To this end, I have created the following Web Service:

[WebService(Namespace = "http://localtest.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class BlogWebService : System.Web.Service.WebService
{
   [WebMethod]
   [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
   public List<Blog> GetLatestBlogs(int noBlogs)
   {
      return GetLatestBlogs(noBlogs)
         .Select(b => b.ToWebServiceModel())
         .ToList();
   }
}

I have tested this on the local server by opening

http://localhost:55671/WebServices/BlogWebService.asmx?op=GetLatestBlogs

and it works correctly.

When I try to access this service remotely and get an Internal Server Error. For example, I have run the following code using LinqPad (based on some script from http://geekswithblogs.net/JuanDoNeblo/archive/2007/10/24/json_in_aspnetajax_part2.aspx):

void Main()
{
   GetLatestBlogs().Dump();
}

private readonly static string BlogServiceUrl =
   "http://localhost:55671/BlogWebService.asmx/GetLatestBlogs?noBlogs={0}";

public static string GetLatestBlogs(int noBlogs = 5)
{
   string formattedUri = String.Format(CultureInfo.InvariantCulture,
      BlogServiceUrl, noBlogs);

   HttpWebRequest webRequest = GetWebRequest(formattedUri);
   HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
   string jsonResponse = string.Empty;
   using (StreamReader sr = new StreamReader(response.GetResponseStream()))
   {
      jsonResponse = sr.ReadToEnd();
   }
   return jsonResponse;
}

private static HttpWebRequest GetWebRequest(string formattedUri)
{
   Uri serviceUri = new Uri(formattedUri, UriKind.Absolute);
   return (HttpWebRequest)System.Net.WebRequest.Create(serviceUri);
}

I have a number of questions/doubts:

  1. How should the call to the web service be formatted? I’m not sure the construction of my BlogServiceUrl in the LinqPad test code is correct.
  2. Are my BlogWebService class and GetBlogs() method defined and attributed correctly?
  3. Do I need to to add anything to my web site configuration to make this work?

Any advice would be much appreciated.

  • 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-18T07:54:36+00:00Added an answer on June 18, 2026 at 7:54 am

    I was unable to find a way to get the HttpHandler (.ashx) mechanism working for remote JavaScript calls – I always received the ‘Invalid referrer’ error in the remote jQuery call. I’m not saying it is not possible, but I could not find any documentation on how to do it – not that worked anyway.

    In the end, I converted the code to a WCF service guided by this article by Ben Dewey – which was very helpful – make sure you apply the class and method attributes carefully – I didn’t and spent a happy hour or so trying to figure out what was wrong!

    Here’s the code:

    IBlogService.cs

    [ServiceContract]
    public interface IBlogService
    {
       [OperationContract]
       List<WebServiceModels.Blog> GetLatestBlogs(int noItems);
    }
    

    BlogService.svc

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class BlogService 
       : Ninject.Web.WebServiceBase, IBlogService
    {
    
       private readonly IScoRepository ScoDb;       // Data repository
    
    
       public BlogService(IScoRepository scoDb)
       {
          this.ScoDb = scoDb;
       }
    
    
       [WebGet(ResponseFormat = WebMessageFormat.Json)]
       public List<WebServiceModels.Blog> GetLatestBlogs(int noItems)
       {
          return ScoDb
             .GetLatestBlogs(noItems)
             .Select(b => b.ToWebServiceModel(hostAddress))
             .ToList();
       }
    }
    

    Web.config

    <system.serviceModel>
       <serviceHostingEnvironment multipleSiteBindingsEnabled="true"  aspNetCompatibilityEnabled="true" />
       <behaviors>
          <endpointBehaviors>
             <behavior name="webHttpBehavior">
                <webHttp />
             </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
             <behavior name="">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
             </behavior>
          </serviceBehaviors>
       </behaviors>
       <bindings>
          <webHttpBinding>
             <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
          </webHttpBinding>
       </bindings>
       <services>
          <service name="WebServices.JSON.BlogService">
             <endpoint name="WebServices.JSON.IBlogService" address="" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="WebServices.JSON.IBlogService" behaviorConfiguration="webHttpBehavior" />
          </service>
       </services>
    </system.serviceModel>
    

    Test JavaScript

    $.ajax({
       type: "GET",
       url: "http://localhost:55671/WebServices/JSON/BlogService.svc/GetLatestBlogs",
       data: {
          noItems: 3
       },
       dataType: "jsonp",
       cache: false,
       success: function(result) {
          alert(result[0].Title + " (" + result.length + " Blogs returned)");
       },
       error: function(jqXHR, textStatus, errorThrown) {
         alert(errorThrown);
       }
    });
    

    As you can see, I also used Ninject DI. This was problematical until I came across the Ninject.Extension.Wcf module which made life very easy. Couple of gotchyas though.

    • I couldn’t find much documentation

    • Add the ‘factory’ attribute to the WCF page:

      Factory=”Ninject.Extensions.Wcf.NinjectWebServiceHostFactory”

      • Adjust the Web.config to reference the service interface else the binding magic won’t happen.

    I hope it helps.

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

Sidebar

Related Questions

I want add new Feed item on entity persist and update. I write this
I have a website built with ASP.NET (3.5) and want add some level of
I want add my custom sidebar next right column all page. Please check this
I want to use this jar file ( http://sourceforge.net/projects/uirt-j/ ) in a personal project.
I want add UIGestureRecognizerDelegate to UIWebView,but failed. if [self.view addsubView:webView]; So UIWebView is ok,but
I want add label to every row in tableview at right side of the
I have a list of string values that I want add to a hashtable
I have problem, when I want add Node to my GUI from other Thread.
I have numbers in javascript from 01(int) to 09(int) and I want add 1(int)
I used RollingFileAppender. And I want add a blank line to the log when

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.