C# Web API控制器无法识别新添加的方法

C# Web API控制器无法识别新添加的方法,c#,.net,asp.net-web-api,asp.net-web-api2,C#,.net,Asp.net Web Api,Asp.net Web Api2,我正在向另一个Web API控制器添加方法。测试现有的方法之一,我能够达到我的突破点。然而,如果我试着打电话到一个新的,我会得到一个404。我正在使用IIS Express和Postman进行本地测试。下面的例子,你知道这是什么原因吗 当我尝试调用新端点时,这是我收到的响应: {"Message":"No HTTP resource was found that matches the request URI 'http://localhost:53453/api/nisperson/a

我正在向另一个Web API控制器添加方法。测试现有的方法之一,我能够达到我的突破点。然而,如果我试着打电话到一个新的,我会得到一个404。我正在使用IIS Express和Postman进行本地测试。下面的例子,你知道这是什么原因吗

当我尝试调用新端点时,这是我收到的响应:

    {"Message":"No HTTP resource was found that matches the request URI 'http://localhost:53453/api/nisperson/addnewconnection'.",
"MessageDetail":"No action was found on the controller 'NISPerson' that matches the request."}
现有方法:

[HttpPost]
[ActionName("register")]
public ClientResponse PostRegisterPerson(HttpRequestMessage req, PersonModel person)
{
  // This method is getting hit if I call it from Postman
}
[HttpPost]
[ActionName("addnewconnection")]
public ClientResponse PostNewConnection(HttpRequestMessage req, string Email, String FirstName, string LastName)
{
  // This is the new method which when called from Postman cannot be found. 
}
端点:
http://localhost:53453/api/test/register

新增方法:

[HttpPost]
[ActionName("register")]
public ClientResponse PostRegisterPerson(HttpRequestMessage req, PersonModel person)
{
  // This method is getting hit if I call it from Postman
}
[HttpPost]
[ActionName("addnewconnection")]
public ClientResponse PostNewConnection(HttpRequestMessage req, string Email, String FirstName, string LastName)
{
  // This is the new method which when called from Postman cannot be found. 
}
端点:
http://localhost:53453/api/test/addnewconnection

您在方法签名中定义了三个必需的参数(
电子邮件
名字
姓氏
):

路由处理程序无法映射此URI:
http://localhost:53453/api/test/addnewconnection
,因为您没有提供这三个必需的参数

正确的URI(保持方法不变)实际上如下所示:

http://localhost:53453/api/test/addnewconnection?Email=foo&FirstName=bar&LastName=baz
如图所示,在URI内提供这些参数,或在不需要时将其转换为默认值:

[HttpPost]
[ActionName("addnewconnection")]
public ClientResponse PostNewConnection(HttpRequestMessage req, string Email = null, String FirstName = null, string LastName = null)
{
  // This is the new method which when called from Postman cannot be found. 
}
提供默认值将允许您使用原始URI点击您的方法。

您在方法签名中定义了三个必需的参数(
Email
FirstName
LastName
):

路由处理程序无法映射此URI:
http://localhost:53453/api/test/addnewconnection
,因为您没有提供这三个必需的参数

正确的URI(保持方法不变)实际上如下所示:

http://localhost:53453/api/test/addnewconnection?Email=foo&FirstName=bar&LastName=baz
如图所示,在URI内提供这些参数,或在不需要时将其转换为默认值:

[HttpPost]
[ActionName("addnewconnection")]
public ClientResponse PostNewConnection(HttpRequestMessage req, string Email = null, String FirstName = null, string LastName = null)
{
  // This is the new method which when called from Postman cannot be found. 
}

提供默认值将允许您使用原始URI点击您的方法。

原因是它希望在querystring中提供您不提供的其他参数(简单字符串类型)。因此,它试图使用单个参数调用Post,但未能找到它。默认情况下,简单类型是从URI读取的。如果希望从表单正文中读取,请使用FromBody属性。

原因是它希望在querystring中提供您不提供的其他参数(简单字符串类型)。因此,它试图使用单个参数调用Post,但未能找到它。默认情况下,简单类型是从URI读取的。如果您想让它们从表单正文中读取,请使用FromBody属性。

在实际代码中,addnewconnectionAdded引号周围缺少“”。我在向StackOverflow添加问题时输入了一个类型。在声明路由时,我通常使用完全限定的路由[ActionName(“ReIndex”)][route(“api/autocomplete/ReIndex”)],只是为了确保,您是否完全回收了IIS Express?(有时它会让应用程序的旧实例在内存中运行…)我还没有这样做,让我试试。我认为在使用IIS express时没有必要这样做。在实际代码中,除了引号外,AddNewConnection周围缺少“”。我在向StackOverflow添加问题时输入了一个类型。在声明路由时,我通常使用完全限定的路由[ActionName(“ReIndex”)][route(“api/autocomplete/ReIndex”)],只是为了确保,您是否完全回收了IIS Express?(有时它会让应用程序的旧实例在内存中运行…)我还没有这样做,让我试试。我认为在使用IIS express时没有必要这样做。谢谢!我太习惯于传入模型对象了,以至于我没有意识到这是简单类型所必需的。谢谢!我太习惯于传入模型对象了,以至于我没有意识到这是简单类型所必需的。