对于以下两个控制器,/api/container/dasdada
返回“a”。如果我做container?value=hello
,我会得到“你好”。什么可能导致参数绑定不起作用?
public HttpResponseMessage Get()
{
return Request.CreateResponse<string>("a");
}
public HttpResponseMessage Get(string value)
{
return Request.CreateResponse<string>(value);
}
我正在使用Katana主持它:
HttpConfiguration apiConfig = new HttpConfiguration();
apiConfig.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
apiConfig.Formatters.Remove(apiConfig.Formatters.XmlFormatter);
apiConfig.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
app.UseDependencyResolver(resolver)
.UseWebApiWithOwinDependencyResolver(resolver, apiConfig);
app.UseWebApi(apiConfig);
答案 0 :(得分:1)
您需要一个名为id
的参数的方法,以便“id”绑定:
public HttpResponseMessage Get(string id) { return Request.CreateResponse<string>(id); }
访问/ api / container / dasdada
时应返回“dasdada”如果你想要两个参数绑定,你可以有一个方法:
public HttpResponseMessage Get(string id, string value) { return Request.CreateResponse<string>(id + ":" + value); }
对于/ api / container / dasdada?value = hello,这应该返回“dasdada:hello”。