Bernhard Koenig
Bernhard Koenig

Reputation: 1382

Output Atom feed with ASP.NET Web API

In my ASP.NET Web API project I have some standard Edit API calls where XML or JSON output or whatever the caller wants is OK for me. But some calls should return ATOM feeds.

Now I found out that I can achieve an ATOM or RSS output by a custom MediaTypeFormatter in this post: How to generate ATOM and RSS2 feeds with ASP.NET Web API?

But it's not actually what I want as it's still up to the caller to request such an output by an HTTP Accept-header. I want to exclusively return ATOM here, no JSON, no (serialized object as) XML.

Is it possible to do this with Web API? Or would it be better to use a standard web controller for those calls and only implement all other API calls as ApiControllers?

Thanks for your help!

Upvotes: 0

Views: 2687

Answers (2)

RaghuRam Nadiminti
RaghuRam Nadiminti

Reputation: 6793

You can do it with web API too. Sample actions follow.

public HttpResponseMessage GetFeed()
{
    return Request.CreateResponse(HttpStatusCode.OK, feedInstance, "application/atom+xml");
}


 public HttpResponseMessage GetFeed()
 {
    return Request.CreateResponse(HttpStatusCode.OK, feedInstance, feedFormatter, "application/atom+xml");
 }

You can use either one of these.

Upvotes: 7

aaroncatlin
aaroncatlin

Reputation: 3271

I'd add a separate ASP Handler Page (.ashx) for this purpose if you want to force this form of output as the API should generically return what the user requests.

Upvotes: 0

Related Questions