OWIN 路由需要 POST 的(任何)查询字符串?
OWIN Route requires (any) querystring for POST?
在自托管 OWIN 应用程序的 API 控制器中给出以下方法:
[HttpPost]
[Route("~/foo")]
public HttpResponseMessage Foo([FromBody]SomeClass data)
{
//...implementation here
}
发布到以下 URI 不会 调用 Foo()
方法:
http://localhost:8080/foo
http://localhost:8080/foo/
发布到以下 URI 确实会 调用 Foo()
方法:
http://localhost:8080/foo?
http://localhost:8080/foo?bar
http://localhost:8080/foo?bar=baz
我希望能够 post 到 'bare' URI http://localhost:8080/foo
我做错了什么? (当然我在初始化服务的时候调用了MapHttpAttributeRoutes()
)
更新 1
奇怪。我创建了以下(独立的)测试用例:
using Microsoft.Owin.Hosting;
using Owin;
using System;
using System.Collections.Specialized;
using System.Net;
using System.Net.Http;
using System.Web.Http;
class Program
{
static void Main(string[] args)
{
var server = WebApp.Start("http://+:8080", (appBuilder) =>
{
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
appBuilder.UseWebApi(config);
config.EnsureInitialized();
});
var postdata = new NameValueCollection();
postdata["data"] = "testing";
var testurls = new[] {
"http://localhost:8080/foo",
"http://localhost:8080/foo/",
"http://localhost:8080/foo?",
"http://localhost:8080/foo?x",
"http://localhost:8080/foo?x=y",
};
foreach (var url in testurls)
{
try
{
using (var w = new WebClient())
w.UploadValues(url, postdata);
Console.WriteLine($"OK : {url}");
}
catch (Exception ex)
{
Console.WriteLine($"FAIL: {url} ({ex.Message})");
}
}
}
}
public struct MyData
{
public string Data { get; set; }
}
public class TestController : ApiController
{
[HttpPost]
[Route("foo")]
public HttpResponseMessage Foo([FromBody]MyData postdata)
{
Console.WriteLine($"POST: {postdata.Data}");
return new HttpResponseMessage(HttpStatusCode.OK);
}
}
输出:
POST: testing
OK : http://localhost:8080/foo
POST: testing
OK : http://localhost:8080/foo/
POST: testing
OK : http://localhost:8080/foo?
POST: testing
OK : http://localhost:8080/foo?x
POST: testing
OK : http://localhost:8080/foo?x=y
所以我的实际项目中有一些不同之处打破了这一点。现在找出不同之处...
更新 2
。问题出在(客户端)应用程序post访问我的服务,而不是服务本身。
查看我更新的问题。我是个笨蛋。问题出在我的 OWIN 服务的应用程序 posting 中,而不是 OWIN 服务本身。代码是正确的,客户端在构建 post 的 URI 的方式中存在错误。
在自托管 OWIN 应用程序的 API 控制器中给出以下方法:
[HttpPost]
[Route("~/foo")]
public HttpResponseMessage Foo([FromBody]SomeClass data)
{
//...implementation here
}
发布到以下 URI 不会 调用 Foo()
方法:
http://localhost:8080/foo
http://localhost:8080/foo/
发布到以下 URI 确实会 调用 Foo()
方法:
http://localhost:8080/foo?
http://localhost:8080/foo?bar
http://localhost:8080/foo?bar=baz
我希望能够 post 到 'bare' URI http://localhost:8080/foo
我做错了什么? (当然我在初始化服务的时候调用了MapHttpAttributeRoutes()
)
更新 1
奇怪。我创建了以下(独立的)测试用例:
using Microsoft.Owin.Hosting;
using Owin;
using System;
using System.Collections.Specialized;
using System.Net;
using System.Net.Http;
using System.Web.Http;
class Program
{
static void Main(string[] args)
{
var server = WebApp.Start("http://+:8080", (appBuilder) =>
{
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
appBuilder.UseWebApi(config);
config.EnsureInitialized();
});
var postdata = new NameValueCollection();
postdata["data"] = "testing";
var testurls = new[] {
"http://localhost:8080/foo",
"http://localhost:8080/foo/",
"http://localhost:8080/foo?",
"http://localhost:8080/foo?x",
"http://localhost:8080/foo?x=y",
};
foreach (var url in testurls)
{
try
{
using (var w = new WebClient())
w.UploadValues(url, postdata);
Console.WriteLine($"OK : {url}");
}
catch (Exception ex)
{
Console.WriteLine($"FAIL: {url} ({ex.Message})");
}
}
}
}
public struct MyData
{
public string Data { get; set; }
}
public class TestController : ApiController
{
[HttpPost]
[Route("foo")]
public HttpResponseMessage Foo([FromBody]MyData postdata)
{
Console.WriteLine($"POST: {postdata.Data}");
return new HttpResponseMessage(HttpStatusCode.OK);
}
}
输出:
POST: testing
OK : http://localhost:8080/foo
POST: testing
OK : http://localhost:8080/foo/
POST: testing
OK : http://localhost:8080/foo?
POST: testing
OK : http://localhost:8080/foo?x
POST: testing
OK : http://localhost:8080/foo?x=y
所以我的实际项目中有一些不同之处打破了这一点。现在找出不同之处...
更新 2
查看我更新的问题。我是个笨蛋。问题出在我的 OWIN 服务的应用程序 posting 中,而不是 OWIN 服务本身。代码是正确的,客户端在构建 post 的 URI 的方式中存在错误。