【发布时间】:2011-12-19 11:06:28
【问题描述】:
这是我的代码 Default.aspx:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org
/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="Sample001.Default" %>
<script src="jquery-1.6.4.min.js" type="text/javascript"></script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Label ID="lbl1" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="masterlbl" Text="Master" runat="server" />
</td>
<td>
<span class="Mastercs">
<asp:DropDownList ID="ddl1" runat="server"/>
</span>
</td>
<td>
<asp:Label ID="slavelbl" Text="Slave" runat="server" />
</td>
<td>
<span class="slavecs">
<asp:DropDownList ID="ddl2" runat="server" />
</span>
</td>
</tr>
</table>
</div>
</form>
<script type="text/javascript">
$(document).ready(function () {
$('span.Mastercs select').change(function () {
$.ajax({
type: "POST",
url: "http://localhost/Sample001/Default.aspx/MyMethod",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$('#lbl1').text = msg;
}
});
});
});
</script>
</body>
</html>
这是 Web.Config:
<?xml version="1.0"?>
<configuration>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="MyMethod" verb="*" path="*.assq"
type="Sample001.MyHandler,Sample001" preCondition="integratedMode" />
</handlers>
</system.webServer>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
和处理程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Sample001 {
public class MyHandler : IHttpHandler {
public bool IsReusable {
get { return true; }
}
public void ProcessRequest(HttpContext context) {
context.Response.Write("Yeeeeeeeeeeeeeee Like it");
}
}
}
我无法像15Seconds 所说的那样正确配置 IIS 7.0,只能像这样添加我的自定义扩展 (.assq):
加载页面并更改时选择脚本不起作用
这是我的回复:
未知的 Web 方法 MyMethod.
参数名称:methodName
正文 {字体系列:“Verdana”;字体重量:正常;字体大小:
.7em;颜色:黑色;}
p {字体系列:“Verdana”;字体重量:正常;颜色:黑色;边距顶部:-5px}
b {字体系列:“Verdana”;字体粗细:粗体;颜色:黑色;上边距:-5px}
H1 {字体系列:“Verdana”;字体重量:正常;字体大小:18pt;颜色:红色}
H2 {字体系列:“Verdana”;字体重量:正常;字体-
尺寸:14pt;颜色:栗色 }
前 {字体系列:“Lucida 控制台”;字体大小:.9em}
.marker {字体粗细:粗体;颜色:黑色;文字装饰:无;}
.version {颜色:灰色;}
.error {margin-bottom: 10px;}
.expandable { 文字装饰:下划线;字体粗细:粗体;颜色:海军蓝;
光标:手; }
<span><H1>Server Error in '/Sample001' Application.<hr width=100% size=1
color=silver></H1>
<h2> <i>Unknown web method MyMethod.<br>Parameter name: methodName</i>
</h2></span>
<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">
<b> Description: </b>An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more information about the
error and where it originated in the code.
<br><br>
<b> Exception Details: </b>System.ArgumentException: Unknown web method
MyMethod.<br>Parameter name: methodName<br><br>
<b>Source Error:</b> <br><br>
<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code>
An unhandled exception was generated during the execution of the current web request.
Information regarding the origin and location of the exception can be identified using
the exception stack trace below.</code>
</td>
</tr>
</table>
<br>
<b>Stack Trace:</b> <br><br>
<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code><pre>
[ArgumentException: Unknown web method MyMethod.
Parameter name: methodName]
System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs
eventArgs) +897827
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
+80
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&
completedSynchronously) +270
</pre></code>
</td>
</tr>
</table>
<br>
<hr width=100% size=1 color=silver>
<b>Version Information:</b> Microsoft .NET Framework Version:4.0.30319;
ASP.NET Version:4.0.30319.237
</font>
</body>
</html>
<!--
[ArgumentException]: Unknown web method MyMethod.
Parameter name: methodName
at System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender,
EventArgs eventArgs)
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep
.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&
completedSynchronously)
-->
为什么这段代码不起作用? 为什么响应说:Unknown web method MyMethod.? 这是因为配置 IIS 7.0 还是其他原因?
KBoek 帮助找出问题所在: 主要的一个是:这 3 个元素会导致混淆 jQuery:
数据:“{}”, contentType: "应用程序/json; charset=utf-8", 数据类型:“json”,
然后我删除它。
【问题讨论】:
-
你上传的图片坏了。除此之外,如果您能提供实际的错误消息,那将会很有用。
标签: c# jquery asp.net httphandler