You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

现有项目中ASMX WebService运行报错“Request format is unrecognized”求助

解决ASMX WebService "请求格式无法识别" 错误

以下是几种针对性的排查和解决方法:

  • 确认Web.config配置正确性
    确保在<system.web>节点下添加了正确的协议支持配置,覆盖GET和POST请求:

    <system.web>
      <webServices>
        <protocols>
          <add name="HttpGet"/>
          <add name="HttpPost"/>
        </protocols>
      </webServices>
    </system.web>
    

    若项目基于.NET 4.0及以上版本,还需检查是否存在请求验证拦截的情况,可在<system.web>中添加:

    <httpRuntime requestValidationMode="2.0"/>
    
  • 检查WebService方法特性
    确保你的WebService方法标记了[WebMethod]特性;如果需要通过GET方式调用,需额外添加[ScriptMethod(UseHttpGet = true)](需引用System.Web.Script.Services命名空间),示例:

    using System.Web.Services;
    using System.Web.Script.Services;
    
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService] // 若要支持AJAX调用需添加此特性
    public class MyWebService : System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
    
  • 验证调用请求的格式

    • 若通过浏览器直接访问(GET方式),需确保URL格式正确:http://你的域名/MyWebService.asmx/HelloWorld
    • 若通过POST请求调用,需设置正确的Content-Type:
      • 普通表单提交:application/x-www-form-urlencoded
      • JSON格式调用(需WebService标记[ScriptService]):application/json
  • 检查项目的处理程序配置
    对于.NET 4.0+的IIS托管项目,需确保<system.webServer>节点中存在ASMX的处理程序映射:

    <system.webServer>
      <handlers>
        <add name="asmx" path="*.asmx" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" resourceType="Unspecified" />
      </handlers>
    </system.webServer>
    

内容的提问来源于stack exchange,提问作者krickx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 12:54:23