When I view my response I have my json data but at the bottom i’ve got the html of the page? I’m trying to create a JSON response any spot something obvious. All i’ve done is create a blank .aspx page.
protected void Page_Load(object sender, EventArgs e){ Database db = DatabaseFactory.CreateDatabase();
DbCommand cmd = db.GetStoredProcCommand("sp_GET_FEED");
db.AddInParameter(cmd, "@FEED_TYPE_ID", DbType.Int32, 1);
List<NewsItem> _NewsItems = new List<NewsItem>() ;
using (IDataReader r = db.ExecuteReader(cmd))
{
while (r.Read())
{
NewsItem i = new NewsItem();
i.id = r["FEED_ID"].ToString();
i.title = r["TITLE"].ToString();
i.fulltext = r["BODY"].ToString();
i.image = r["IMAGE_URL"].ToString();
i.created = r["DATE_CREATED"].ToString();
i.url = r["URL"].ToString();
_NewsItems.Add(i);
}
}
string json = JsonConvert.SerializeObject(_NewsItems);
//Response.Clear();
Response.AddHeader("Content-type", "text/json");
Response.AddHeader("Content-type", "application/json");
Response.ContentType = "application/json";
Response.Write(json);}
so my resposne is
///////////////////////////////JSON Output which looks correct//////////////////////////////
< html>
balhhhh which I don’t need for my response as it’s should be a feed of JSON data.
< /html>
Try calling
after
Response.Write(). This will prevent the rest of ASP.NET from adding HTML from the page.Alternatively, if you never want to render the actual page, you should probably consider writing an ASP.NET Handler (.ashx) instead of a page (.aspx) in the first place.