C# 如何调用处理程序来发布值
我有一个处理程序StackOverFlow.cs,如下所示:C# 如何调用处理程序来发布值,c#,jquery,c#-4.0,C#,Jquery,C# 4.0,我有一个处理程序StackOverFlow.cs,如下所示: public class StackOverFlow: IHttpHandler { public void ProcessRequest(HttpContext context) { var nameValueCollection = HttpUtility.ParseQueryString(HttpUtility.UrlDecode(encodedUrl)); //.. }
public class StackOverFlow: IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var nameValueCollection = HttpUtility.ParseQueryString(HttpUtility.UrlDecode(encodedUrl));
//..
}
}
我使用ParseQueryString获取QueryString参数
如何使用jquerypost测试它?可能吗
例如,下面的代码使用以.ashx结尾的URL,是否可以与.cs一起使用?
如何触发我的StackOverFlow类,该类用html POST继承IHttpHandler
<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>
$(函数(){
//我们绑定了submitclick函数
$(“#btnSubmitJSON”)。单击(函数(){
var nameValue=$(“#txtName”).val();
var emailValue=$(“#txtEmail”).val();
var contactValue=$(“#txtContactNo”).val();
//我们只需快速检查值是否为空
如果(nameValue!=“”&&emailValue!=“”&&contactValue!=“”){
//我们可以隐藏按钮,只是为了确保用户在进程中没有单击按钮。
$(“#btnSubmitJSON”).show();
//我们可以输出ajax图标加载,以便用户知道正在加载。
$(“#输出”).html(“请稍候,我们正在处理您的请求。”);
//我们构建json字符串
var jsonData={
“名称”:名称值,
“电子邮件”:emailValue,
“联系人”:联系人值
}
//注意:为了正确传递json字符串,必须使用函数json.stringfy
jsonData=JSON.stringify(jsonData);
$.ajax({
url:“/ContactHandler.ashx”,//确保路径正确
cache:false,
键入:“POST”,
资料来源:jsonData,
成功:功能(响应){
//从服务器输出响应
$(“#输出”).html(响应);
//我们清理数据
$(“#txtName”).val(“”);
$(“#txtEmail”).val(“”);
$(“#txtContactNo”).val(“”);
},
错误:函数(xhr、ajaxOptions、thrownError){
$(“#输出”).html(xhr.responseText);
$(“#btnSubmitJSON”).show();
}
})
}否则{
$(“#输出”).html(“请输入所有字段”);
}
});
});
您需要一组测试,一个端到端测试,一个针对伪后端服务的javascript单元测试,以及一个针对您的处理程序的单元测试,我不能给出示例,但这正是需要的,有很多资源用于单元测试C代码、javascript代码和系统端到端测试