简体   繁体   English

从aspx.cs页面的.ashx处理程序调用方法

[英]Calling methods from .ashx handler from aspx.cs page

I'm having a bit of trouble calling a method from a generic handler we have. 我在从通用处理程序中调用方法时遇到麻烦。 I've tried using two separate techniques to call a simple 'HelloWorld()' method but I get two different errors: 我尝试使用两种单独的技术来调用简单的“ HelloWorld()”方法,但遇到两个不同的错误:

The first technique is as follows: 第一种技术如下:

        WebClient wc = new WebClient();

        NameValueCollection formData = new NameValueCollection();
        formData["method"] = "HelloWorld";


        byte[] data;

        try
        {
            data = wc.UploadValues(_domain, formData);
        }
        catch (WebException ex)
        {
            Label1.Text = ex.Message;
            return;
        }

        string response = Encoding.UTF8.GetString(data);
        Label1.Text = response;

        wc.Dispose();

and I get the following error: 我收到以下错误:

{"id":null,"error":{"name":"Found String where Object was expected."}}

and the second technique I've tried is: 我尝试过的第二种技术是:

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(_domain);
        httpWebRequest.ContentType = "text/json";
        httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "{\"method\":\"helloWorld\"}"; //," +
            //"\"password\":\"bla\"}";

            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();

            try
            {
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                }
            }
            catch (WebException wex)
            {
                Label2.Text = wex.Message;
            }
            catch (Exception ex)
            {
                Label2.Text = ex.Message;
            }
        }

and with this, I get the following error: 并由此得到以下错误:

The remote server returned an error: (500) Internal Server Error.

When I test the call from the ".ashx?test" page the method runs and the details at the bottom of the screen are: 当我从“ .ashx?test”页面测试呼叫时,该方法将运行,并且屏幕底部的详细信息如下:

Pragma: no-cache
Date: Tue, 23 Jul 2013 13:46:19 GMT
Server: ASP.NET Development Server/11.0.0.0
X-AspNet-Version: 2.0.50727
Content-Type: application/json; charset=utf-8
Cache-Control: no-cache
Connection: Close
Content-Length: 32
Expires: -1

Any ideas as to why this wouldn't be working? 关于为什么这行不通的任何想法?

Thanks! 谢谢!

An ASHX handler is not a web service. ASHX处理程序不是Web服务。 You don't call methods within the ASXH handler. 您不会在ASXH处理程序中调用方法。 You just call the handler, and it delivers data directly, be it a text or binary data - that's up to you. 您只需要调用处理程序,它就可以直接传递数据(是文本数据还是二进制数据),这取决于您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM