4

Possible Duplicate:
JSONP with MVC 4 WebApi

I have a get method for my WebAPI which is as follows:

    private T Get<T>(string uri)
    {
        T result = default(T);
        bool isSuccess = true;

        client
            .GetAsync(uri)
            .ContinueWith(task =>
            {
                // EnsureStatus
                isSuccess = task.Result.IsSuccessStatusCode;
                task
                .Result
                .Content
                .ReadAsAsync<T>()
                .ContinueWith(t => result = t.Result)
                .Wait();
            })
            .Wait();
       return result;
     }

The result is produced in a JSON format but I want it for JSONP.

I have read that ReadAsSync only handles built in mediaformatters. So is there a way I can change it to handle JsonP?

Community
  • 1
  • 1
phoenix
  • 103
  • 2
  • 6
  • Why would you need JSONP on a .NET client? – Aliostad May 09 '12 at 12:07
  • Quite often JSONP is used to pull data across domains. – EBarr May 09 '12 at 12:37
  • @EBarr yes, in javascript and in browser. Not when you have a full-blown .NET client. The server must be able to serve json if client asks for `application/json`. This is a **content negotiation** issue. – Aliostad May 09 '12 at 12:42
  • @EBarr - You are right! I do not have a full-blown .NET client. So it is a content negotiation issue. Adding an extra method to handle Jsonp could be helpful. I read about JsonpMediaTypeFormatter and it should be added to the Application_Start of global.asax. But we are not using a global.asax to start with. – phoenix May 09 '12 at 13:33

1 Answers1

4

Stealing liberally from this duplicate....

To accomplish what you want you need three things :

  1. to add a media formatter that outputs JSONP
  2. register the media formatter (traditionally done through global.asx)
  3. ensure the client requests jsonP.

You can steal this JSONP media formatter.

Then, you need to register the media formatter. You can do this programatically with the following code snippet:

var config = GlobalConfiguration.Configuration;
config.Formatters.Insert(0, new JsonpMediaTypeFormatter());

Since you apparently don't use global.asax you're going to need to make sure the formatter is registered somehow. YOU don't provide enough information on how to do it, but i suspect a judiciously placed IF statement and a static variable indicating registration would get you there.

I still don't quite know what type of client you're using, but if it's jquery something like the following will get you there:

$.ajax({
    url: 'http://myurl.com',
    type: 'GET',
    dataType: 'jsonp',
    success: function (data) {
        alert(data.MyProperty);
    }
})

The important part is the accept header sent matches the accept header your shiny new jsonp formatter is listening for. The top two choices in my opinion are either: application/javascript or text/javascript.

Community
  • 1
  • 1
EBarr
  • 11,826
  • 7
  • 63
  • 85
  • Its a selfhosting server so we have something like: var config = new HttpSelfHostConfiguration("http://localhost:1234"); server = new HttpSelfHostServer(config); – phoenix May 09 '12 at 14:51
  • I haven't worked with the self hosted stuff yet, but i suspect you need to add some properties to that `config` object you're passing in to `HttpSelfHostServer` – EBarr May 09 '12 at 18:11
  • Agreed. And It should be something like: config.Formatters[0] = new JsonpMediaTypeFormatter(); However this is not the case. HttpSelfHostConfiguration derives from HttpConfiguration that doesnt have the set property for Formatter. Thank you! – phoenix May 09 '12 at 18:42
  • Ok. Got it all working! Thanks for your help! I was missing the System.Net.Http.Formatting dll because of which I could not add JsonpMediaTypeFormatter to the config. – phoenix May 10 '12 at 08:59
  • The JsonP media formatter is now part of WebApiContrib.Formatting.Jsonp in NuGet. – Jon Onstott Oct 31 '12 at 04:03