0

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?

1
  • For this I would use a generic handler (.ashx). Commented Aug 13, 2013 at 13:30

4 Answers 4

5

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
Sign up to request clarification or add additional context in comments.

6 Comments

I've used this approach plenty of times, it works great. Remember to add the reference to your web config though. msdn.microsoft.com/en-us/library/ms227673(v=vs.100).aspx
@asawyer: There is no need to register anything on web.config for his scenario ;) Just need to create the handler and use it.
Is there really a benefits in this approach versus a simple SVC file that describe a restful web service? Or compared to a ASP.Net Web API Project?
@Steve B: the main advantage I see over those methods you mention is simplicity. It's extremelly simple to implement a web handler inside an existing asp.net web forms project.
@ClaudioRedi That's interesting, in my projects using this technique I've always needed to register the module.
|
0

Look into to ASP.Net Web Methods

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

Comments

0

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();

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.