相关疑难解决方法(0)

使用字符串参数调用web api

我有一个web api,我有2个方法,一个没有参数,两个有不同类型的参数(字符串和整数).调用字符串方法时它不起作用...我在这里缺少什么?

public class MyControllerController : ApiController
{
    public IHttpActionResult GetInt(int id)
    {
        return Ok(1);
    }

    public IHttpActionResult GetString(string test)
    {
        return Ok("it worked");
    }
}
Run Code Online (Sandbox Code Playgroud)

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 }
    );
}
Run Code Online (Sandbox Code Playgroud)

我的电话:

/ api/MyController/MyString //不起作用

/ api/MyController/1 //工作

我收到以下错误:

参数字典包含'TestAngular.Controllers.MyControllerController'中方法'System.Web.Http.IHttpActionResult GetInt(Int32)'的非可空类型'System.Int32'的参数'id'的空条目.可选参数必须是引用类型,可空类型,或者声明为可选参数.

我的请求中缺少什么?

c# asp.net-web-api asp.net-web-api2

9
推荐指数
3
解决办法
5万
查看次数

标签 统计

asp.net-web-api ×1

asp.net-web-api2 ×1

c# ×1