[CLOSED] Correct DirectResponse from an ashx handler

  1. #1

    [CLOSED] Correct DirectResponse from an ashx handler

    Hi,

    I'm sure this has been answered before so apologies if so, but I searched quite a bit and didn't find it.

    Consider the following:

    ASPX:

    <%@ Page Language="C#" %>
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>DirectEvent Request tests</title>
    
        <script type="text/javascript">
            function doRequest(simulation) {
                Ext.net.DirectEvent.request({
                    url            : 'DirectRequestTest.ashx',
                    cleanRequest: true,
                    extraParams    : { success : simulation },
                    eventMask    : { showMask:true },
                    success        : function(result) { console.log('success', result); },
                    failure        : function() { console.log('failure', arguments); }
                });
            }
        </script>
    </head>
    <body>
        <ext:ResourceManager ID="ResourceManager1" runat="server" />
        <form id="HtmlForm" runat="server">
        <div>
            <ext:Button ID="Request1" Text="Request a Success" runat="server">
                <Listeners>
                    <Click Handler="doRequest(true);" />
                </Listeners>
            </ext:Button>
    
            <ext:Button ID="Request2" Text="Request a Failure" runat="server">
                <Listeners>
                    <Click Handler="doRequest(false);" />
                </Listeners>
            </ext:Button>
        </div>
        </form>
    </body>
    </html>
    ASHX:
    using System.Web;
    
    namespace Ext.Net.Tests.Events
    {
        public class DirectRequestTest : IHttpHandler
        {
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "application/json";
    
                var response = new DirectResponse
                {
                    Success = context.Request["success"] == "true"
                };
    
                if (response.Success)
                {
                    response.Result = "some result";
    
                    var parameters = new ParameterCollection
                    {
                        new Parameter("test", "value")
                    };
    
                    response.ExtraParamsResponse = parameters.ToJson();
                }
                else
                {
                    response.ErrorMessage = "Request failed";
                }
    
                response.Return();
            }
    
            public bool IsReusable
            {
                get { return false; }
            }
        }
    }
    Questions:
    1. What am I doing wrong here (the success handler doesn't seem to fire though I can see the response in the http traffic)?
    2. When the failure request is simulated how do I prevent the default request failure window appearing (I found some examples of using Ext.net.Direct.Request() but while they had a success handler, they didn't have failure handlers. Should I be doing it another way?)
    3. Should I even bother using an ashx for this? Should I use a WebService (asmx) instead like this: https://examples1.ext.net/#/Events/D...ds/WebService/ ? [I am in a scenario where I can't assume a Page so can't use the nice [DirectMethod] approach which I'd normally have done here.] Is there any performance difference?

    Thanks!
    Last edited by Daniil; Jun 30, 2011 at 9:37 AM. Reason: [CLOSED]
  2. #2
    Doh! Ignore. Sorry to waste your time. (You can mark as closed)

    I made a mistake of using Ext.net.DirectEvent.request instead of DirectMethod

    In addition, after changing to DirectMethod, extraParams had to be params.

    Here's a slightly updated bit of code (mostly for my own future reference!)

    <%@ Page Language="C#" %>
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>DirectEvent Request tests</title>
    
        <script type="text/javascript">
            function doRequest(simulation) {
                Ext.net.DirectMethod.request({
                    url            : 'DirectRequestTest.ashx',
                    cleanRequest: true,
                    params        : { success : simulation },
                    eventMask    : { showMask: true },
                    success        : function(result, response, extraParams, o) { console.log('success', result, response, extraParams, o); },
                    failure        : function(errorMessage, response, extraParams, o) { console.log('failure', arguments); },
                    complete    : function(result, errorMessage, response, extraParams, o) { console.log('complete', result, errorMessage, response, extraParams, o); },
                    showFailureWarning : false
                });
            }
        </script>
    </head>
    <body>
        <ext:ResourceManager ID="ResourceManager1" runat="server" />
        <form id="HtmlForm" runat="server">
        <div>
            <ext:Button ID="Request1" Text="Request a Success" runat="server">
                <Listeners>
                    <Click Handler="doRequest(true);" />
                </Listeners>
            </ext:Button>
    
            <ext:Button ID="Request2" Text="Request a Failure" runat="server">
                <Listeners>
                    <Click Handler="doRequest(false);" />
                </Listeners>
            </ext:Button>
        </div>
        </form>
    </body>
    </html>
    Last edited by anup; Jun 30, 2011 at 9:31 AM. Reason: Fixed typo
  3. #3
    Quote Originally Posted by anup View Post
    1. What am I doing wrong here (the success handler doesn't seem to fire though I can see the response in the http traffic)?
    2. When the failure request is simulated how do I prevent the default request failure window appearing (I found some examples of using Ext.net.Direct.Request() but while they had a success handler, they didn't have failure handlers. Should I be doing it another way?)
    Hi,

    Please use "useSuccess" and "userFailure" when you use Ext.net.DirectEvent.request().

    For example, if you'd define in the markup:
    <DirectEvents>
        <Click 
            OnEvent="TestDirectEventHandler"
            Success="alert('success')"
            Failure="alert('failure')" />
    </DirectEvents>
    you will get it the page sources:
    directEvents: {
        click : {
            fn : function (item, e) {
                var params = arguments;
                Ext.net.DirectEvent.confirmRequest({
                    userSuccess : function (response, result, el, type, action, extraParams, o) {
                        alert('success')
                    },
                    userFailure : function (response, result, el, type, action, extraParams, o) {
                        alert('failure')
                    },
                    control: this
                });
            },
            delay: 20
        }
    }
    Generally speaking, DirectRequest is not for "public" usage (in JavaScript), so, we recommend to use DirectMethod.

    You can use "success" and "failure" in DirectMethod's config.
    Last edited by Vladimir; Jun 30, 2011 at 10:55 AM.
  4. #4
    Quote Originally Posted by Daniil View Post
    [/LIST]

    Hi,

    Please use "useSuccess" and "userFailure" when you use Ext.net.DirectEvent.request().
    lol Daniil, looks like you and I replied at almost exactly the same time :)

    Many thanks for that suggestion, as that is also very useful to know.

    Anup
  5. #5
    :)

    A bit later I updated the post with:
    Generally speaking, DirectMethod is not for "public" usage (in JavaScript), so, we recommend to use DirectMethod.

    You can use "success" and "failure" in DirectMethod's config.
  6. #6
    Quote Originally Posted by anup View Post
    Should I even bother using an ashx for this? Should I use a WebService (asmx) instead like this: https://examples1.ext.net/#/Events/D...ds/WebService/ ? [I am in a scenario where I can't assume a Page so can't use the nice [DirectMethod] approach which I'd normally have done here.] Is there any performance difference?
    We think that the performance difference will be minor. But...to guarantee it with sure the full performance testing is required:)
  7. #7
    Quote Originally Posted by Daniil View Post
    :)

    A bit later I updated the post with:

    Generally speaking, DirectMethod is not for "public" usage (in JavaScript), so, we recommend to use DirectMethod.

    You can use "success" and "failure" in DirectMethod's config.
    You accidentally typed DirectMethod twice. One of them I guess is DirectEvent?
  8. #8
    Yes, apologize, the first "DirectMethod" must be "DirectEvent":

    Generally speaking, DirectEvent is not for "public" usage (in JavaScript), so, we recommend to use DirectMethod.

    You can use "success" and "failure" in DirectMethod's config.
  9. #9
    Many thanks. In which case, my second post that corrected my original usage works for me quite well.

Similar Threads

  1. Replies: 2
    Last Post: Jun 03, 2012, 4:18 PM
  2. Replies: 0
    Last Post: Mar 21, 2011, 3:55 PM
  3. Add controls via MVC Controller (DirectResponse?)
    By peter.campbell in forum 1.x Help
    Replies: 2
    Last Post: Jan 28, 2011, 8:33 AM
  4. [CLOSED] Local ASHX and XML in Store in an MVC
    By fondant in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: May 20, 2009, 8:22 AM
  5. IHttpHandler (ashx)
    By Zarzand in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Jan 09, 2009, 5:32 AM

Posting Permissions