c# - 找不到 ASP.NET Web API 路由 Controller

标签 c# asp.net asp.net-mvc asp.net-web-api asp.net-web-api-routing

我正在尝试发布到以下 Web API:

http://localhost:8543/api/login/authenticate

LoginApi (Web API) 定义如下:

[RoutePrefix("login")]
public class LoginApi : ApiController
{
    [HttpPost]
    [Route("authenticate")]
    public string Authenticate(LoginViewModel loginViewModel)
    {  
        return "Hello World"; 
    }
}

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 }
    );
}

这是我得到的错误:

Request URL:http://localhost:8543/api/login/authenticate
Request Method:POST
Status Code:404 Not Found
Remote Address:[::1]:8543

最佳答案

您的 Controller 名称“LoginApi”需要以“Controller”结尾,以便框架找到它。例如:“登录 Controller ”

这是一篇很好的文章,它解释了 ASP.NET Web API 中的路由:http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

关于c# - 找不到 ASP.NET Web API 路由 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36209626/

相关文章:

c# - 在线更新一个C#程序

c# - 在不需要时使用异步等待的性能影响

c# - XmlDocument.Load 失败,LoadXml 有效 :

c# - 当前上下文中不存在名称 Response

asp.net - 为什么 session 自动结束

asp.net - Webmatrix - WebGrid : change css style conditionally

.net - Global.asax 中的 ASP.NET MVC Application_Error 处理程序

jquery - 用于 jQuery Mobile 控件的 Asp.Net MVC Razor 标记

c# - ELMAH 不从本地主机发送电子邮件

c# - "C# base class virtual function"- "override in Managed C++ ref class"