Adam Haile
Adam Haile

Reputation: 31339

ASP.NET JSON response without web service

I've got an ASP.NET based website that already has the necessary hooks into my back-end database and I want to write something really simple to return a small bit of JSON based on a URL parameter. So, for example:

http://example.com/JSON/GetInfo.aspx?prodID=1234

And it would return some JSON with product details for the given ID.

I conceptually know how to do this from any ASPX page but I'm wondering if this is the right way to do it? (Assuming I would just be writing JSON back out to the response instead of HTML)

I don't need (or want) a full on .NET web service, just something that I could call from other pages on my site as well as one of our applications with a GET request to retrieve the desired info.

In visual studio, when I'm adding a new file what type should I use?

Upvotes: 0

Views: 1709

Answers (4)

rom
rom

Reputation: 3

Create your aspx page "GetInfo.aspx" In your Page_Load :

string myID = request.QueryString["prodID"];
string myJson = "";

//Fill your JSON with database, ...

Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.Write(myJson);
Response.End();

That will returning your JSON

Upvotes: 0

markiemooster
markiemooster

Reputation: 77

Wouldn't an ordinary .aspx file do this?

string s= "{\"data\":\"test\"}";
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.Write(s);
Response.End();

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68400

You should use a Generic Web Handler for this. It's a lightweight web component that you can call from any client code on your site.

Your url would looks like this

http://example.com/JSON/GetInfo.ashx?prodID=1234

Upvotes: 5

AntSpiteri
AntSpiteri

Reputation: 312

Look into to ASP.Net Web Methods

http://msdn.microsoft.com/en-us/library/byxd99hx(v=vs.90).aspx

Upvotes: 0

Related Questions