开发者

Printing json with C# asp.net

开发者 https://www.devze.com 2023-02-27 20:53 出处:网络
Alright, so I have some jQuery code that will send an AJAX request to an aspx file. My Spellchecker.aspx file looks like this:

Alright, so I have some jQuery code that will send an AJAX request to an aspx file.

My Spellchecker.aspx file looks like this:

<%@ Page Languag开发者_如何转开发e="C#" AutoEventWireup="true" CodeFile="Spellchecker.aspx.cs" Inherits="Spellchecker" %>
<head id="Head1" runat="server" />

I had to put this "head" tag, otherwise I get an error regarding "<page theme" in web.config file (which I need for other pages in the site). This means that the response from server comes in the form of: <JSON HERE><head .../> which is wrong because the code should just return json data.

In the aspx.cs file, I am returning a dictionary-converted-to-json in Page_Load:

dict.Add("just_json", json_obj);
JavaScriptSerializer serializer = new JavaScriptSerializer(); //creating serializer instance of JavaScriptSerializer class
string json = serializer.Serialize((object)dict);

Response.Write(json);
}

So in an alert box, I see json data, followed by <head id="Head1"><link href..." stylesheets etc.

How can I make it so only JSON data is returned from aspx?

UPDATE: I think I figured this out. Putting Theme="" in the "% Page" tag in the aspx file seems to disable the theme!


To answer your actual question of "why do I need the head element" - because this is where ASP.NET puts your links to CSS and some JavaScript imports.

It's unclear exactly what you're trying to do here but it looks like you probably want to create a Web Service or expose a method as a ScriptMethod. It is strange to use an ASPX page for the purpose of outputting a response to an AJAX request.

Look into ScriptMethods or HttpHandlers.

HttpHandlers allow you to manage the response completely. So you would create a handler and hook it to "SpellChecker.ashx" and the handler could write directly to the response stream.

public class SpellCheckerHttpHandler : IHttpHandler
{
    public bool IsReusable { get { return true; } }

    public void ProcessRequest(HttpContext context)
    {
        //Write out the JSON you want to return.
        string json = GetTheJson();

        context.Response.ContentType = "application/json";
        context.Response.Write(json);
    }
}

And then, in your Web.Config inside the system.webServer element, add:

<handlers>
    <add name="SpellChecker" path="~/SpellChecker.ashx" type="MyNamespace.HttpHandlers.SpellCheckerHttpHandler, MyAssembly" />
</handlers>

Now you can make a request to your handler like http://localhost/SpellChecker.ashx?TextToCheck=xyz.


If all your page is doing is processing input and outputting JSON, consider using a "Generic Handler" page that ends in ashx instead of a "Web Page" that ends in asmx. It has a lot less over head, and won't try loading themes, etc.

You can get it to output JSON instead of XML or something else by controlling the Content-Type of the output:

context.Response.ContentType = "application/json";
0

精彩评论

暂无评论...
验证码 换一张
取 消