【发布时间】: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