【问题标题】:ASP.NET WebService not hit, parameter stringASP.NET WebService 未命中,参数字符串
【发布时间】:2016-09-27 02:05:27
【问题描述】:

我有以下操作代码来接受服务请求:

namespace MyStuff.api
{
    [RoutePrefix("api/Dy")]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    [Authorize]
    public class DyController : ApiController
    {
        //...
        [Route("{pit}/{name}")]
        public void Post(string pit, string name, [FromBody]JObject obj)
        {
            //...
            #region do something
            // work with name here
            #endregion
        }
        //...
    }
}
测试场景

我在调试器中,并且在 Post 方法内的某处设置了一个断点。在数据集中,我找到了一个品牌名称“3.1 Phillip Lim”。我试图弄清楚如何操纵这个名称(以及其他类似的创意名称),以便 Post 方法被命中。

断点未命中 - 请求未到达 Post 方法
- $.post("http://undisclosed.server.net/api/Dy/Brands/3.1%20Phillip%20Lim", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; })
- $.post("http://undisclosed.server.net/api/Dy/Brands/-3.1%20Phillip%20Lim", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; })
- $.post("http://undisclosed.server.net/api/Dy/Brands/id-3.1%20Phillip%20Lim", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; })
- $.post("http://undisclosed.server.net/api/Dy/Brands/body.Name", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; })
- $.post("http://undisclosed.server.net/api/Dy/Brands/{body.Name}", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; })
断点命中 - 请求按预期到达
$.post("http://undisclosed.server.net/api/Dy/Brands/body-Name", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; })
$.post("http://undisclosed.server.net/api/Dy/Brands/{null}", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; })

WebApi 2 显然在幕后做了一些事情——可能是对 URL 中的段的某种类型识别...

请解释发生了什么……或者你是如何解决这个问题的。谢谢。

【问题讨论】:

    标签: c# jquery asp.net asp.net-web-api


    【解决方案1】:

    在我看来,您的问题与路由参数中的点字符直接相关。这是一个已知问题,取决于 IIS/Web API 将您的查询解释为静态文件请求(因为它以点 + 似乎是扩展名的内容结尾)。

    你可以尝试什么:

    • 在您的请求 URI 中添加尾部斜杠:

      http://undisclosed.server.net/api/Dy/Brands/3.1%20Phillip%20Lim/.

      这应该足以让 Web API 处理请求,但我必须承认这更像是一种解决方法。根据this question,您可以在 IIS 中创建重写以使其成为默认行为。

    • RAMMFAR 添加到您的 Web.config 文件中:

      <configuration>
          <system.webServer>
              <modules runAllManagedModulesForAllRequests="true" />
          </system.webServer>
      </configuration>
      

      这可能就足够了,但如果它不能单独使用,请结合以下方法使用。还要注意performance implications when using RAMMFAR

    • 为您的Web.config 的任何路径(*)添加ExtensionlessUrlHandler

      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0"  path="*" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
      
    • 干脆放弃,永远不要在尾随路由参数中使用点。通常我会尝试将可能包含 URI 不友好字符(如点、空格和任何必须编码的字符)的参数移动到查询字符串中。另一种选择是简单地将这些字符替换为对 URI 更友好的字符(例如破折号)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多