0

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>

3 Answers 3

3

Try calling

Response.End();

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.

Sign up to request clarification or add additional context in comments.

Comments

2

This is because you are rendering your json inside a page object, try doing it in a Http Handler instead.

Comments

0

I believe you haven't removed the dummy markup in your aspx file. just remove and try again. It will be ok.

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.