如何调用处理程序来发布值

时间:2013-12-24 16:11:40

标签: c# jquery c#-4.0

我有一个像下面这样的处理程序StackOverFlow.cs:

public class StackOverFlow: IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        var nameValueCollection = HttpUtility.ParseQueryString(HttpUtility.UrlDecode(encodedUrl));

        //..
    }
}

我使用ParseQueryString获取QueryString参数。

如何用jquery帖子测试它?有可能吗?

例如下面的代码使用结束.ashx的URL,是否可以使用.cs? 如何通过html POST触发继承IHttpHandler的StackOverFlow类?

<script type="text/javascript">
    $(function () {
        //we bind the submit click function
        $("#btnSubmitJSON").click(function(){
            var nameValue = $("#txtName").val();
            var emailValue = $("#txtEmail").val();
            var contactValue = $("#txtContactNo").val();

            //we just use a quick check if the value are empty or not
            if(nameValue != "" && emailValue != "" && contactValue != ""){
                //we can hide the button just to make sure user does not click the button during the progress.
                $("#btnSubmitJSON").show();

                //we can output ajax icon loading so the user know it is in progress.
                $("#output").html("<img src=\"/content/images/ajax-loader.gif\" /> Please wait, we are processing your request.");

                //we build the json string
                var jsonData = {
                    'Name': nameValue,
                    'Email': emailValue,
                    'Contact': contactValue
                }

                //note in order to proper pass a json string, you have to use function JSON.stringfy
                jsonData = JSON.stringify(jsonData);

                $.ajax({
                    url: "/ContactHandler.ashx", //make sure the path is correct
                    cache: false,
                    type: 'POST',
                    data: jsonData,
                    success: function (response) {
                        //output the response from server
                        $("#output").html(response);

                        //we clean up the data
                        $("#txtName").val("");
                        $("#txtEmail").val("");
                        $("#txtContactNo").val("");
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        $("#output").html(xhr.responseText);
                        $("#btnSubmitJSON").show();
                    }
                })
            }else{
                $("#output").html("Please enter all fields.");
            }
        });
    });
</script>

1 个答案:

答案 0 :(得分:0)

你需要一组测试,一个端到端的测试,一个针对虚假后端服务的javascript的单元测试以及一个针对你的处理程序的单元测试,我不能举例但是这就是需要的,有很多资源用于单元测试C#代码,javascript代码和系统端到端测试