使用字符串参数调用网络 api
call to web api with string parameter
我有一个 Web api,其中有 2 个方法,一个没有参数,两个有不同类型的参数(字符串和整数)。调用字符串方法时它不起作用...我在这里缺少什么?
public class MyControllerController : ApiController
{
public IHttpActionResult GetInt(int id)
{
return Ok(1);
}
public IHttpActionResult GetString(string test)
{
return Ok("it worked");
}
}
WebApiConfig.cs:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
我的电话:
/api/MyController/MyString //Doesnt work
/api/MyController/1 //work
我收到以下错误:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Http.IHttpActionResult GetInt(Int32)' in 'TestAngular.Controllers.MyControllerController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
我的请求中缺少什么?
您必须更改您的 uri
/api/MyController
/api/MyController/string
/api/MyController/1
您不必指定方法。
您可以在 asp.net 上查看此 tutorial 以获得进一步的说明。
此 uri 也应该有效:
api/MyController/GetAll
api/MyController/GetString?param=string
api/MyController/GetInt?param=1
我认为这更清楚并且应该始终有效。
您使用路由行为。
看这里:
http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection
你发布这篇文章已经有一段时间了,但我想我有答案了。
首先,有两个问题。首先,正如 Pinback 指出的那样,您不能对两个不同的端点使用相同的路由。
但是,如果您只是删除 int
方法,您仍然会 运行 遇到问题。
请记住:默认路由如下所示:api/{controller}/{id}
为了绑定参数,必须调用 "id",而不是 "test".
将签名更改为:
public IHttpActionResult GetString(string id)
它会起作用的。
(您也可以在 webapiconfig.cs 文件中将 {id}
更改为 {test}
。
这是我的解决方案:
不更改 webapiconfig.cs 文件
中的默认路由
只添加一个路由到您的字符串函数:
[Route("Api/MyController/GetString/{test}")]
public IHttpActionResult GetString(string test)
http://localhost:49609/api/MyController/GetString/stringtest
body中也可以带字符串参数
string body;
using (var sr = new StreamReader(Request.Body))
body = sr.ReadToEnd();
我有一个 Web api,其中有 2 个方法,一个没有参数,两个有不同类型的参数(字符串和整数)。调用字符串方法时它不起作用...我在这里缺少什么?
public class MyControllerController : ApiController
{
public IHttpActionResult GetInt(int id)
{
return Ok(1);
}
public IHttpActionResult GetString(string test)
{
return Ok("it worked");
}
}
WebApiConfig.cs:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
我的电话:
/api/MyController/MyString //Doesnt work
/api/MyController/1 //work
我收到以下错误:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Http.IHttpActionResult GetInt(Int32)' in 'TestAngular.Controllers.MyControllerController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
我的请求中缺少什么?
您必须更改您的 uri
/api/MyController
/api/MyController/string
/api/MyController/1
您不必指定方法。
您可以在 asp.net 上查看此 tutorial 以获得进一步的说明。
此 uri 也应该有效:
api/MyController/GetAll
api/MyController/GetString?param=string
api/MyController/GetInt?param=1
我认为这更清楚并且应该始终有效。 您使用路由行为。
看这里: http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection
你发布这篇文章已经有一段时间了,但我想我有答案了。 首先,有两个问题。首先,正如 Pinback 指出的那样,您不能对两个不同的端点使用相同的路由。
但是,如果您只是删除 int
方法,您仍然会 运行 遇到问题。
请记住:默认路由如下所示:api/{controller}/{id}
为了绑定参数,必须调用 "id",而不是 "test".
将签名更改为:
public IHttpActionResult GetString(string id)
它会起作用的。
(您也可以在 webapiconfig.cs 文件中将 {id}
更改为 {test}
。
这是我的解决方案: 不更改 webapiconfig.cs 文件
中的默认路由只添加一个路由到您的字符串函数:
[Route("Api/MyController/GetString/{test}")]
public IHttpActionResult GetString(string test)
http://localhost:49609/api/MyController/GetString/stringtest
body中也可以带字符串参数
string body;
using (var sr = new StreamReader(Request.Body))
body = sr.ReadToEnd();