c# - HTTPPost MVC 4 网络 API

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

我正在构建一个 mvc 4 web api,但是每当我尝试向 web api 发送帖子时,请求都会返回

"The requested resource does not support http method 'POST'."

我的请求头

User-Agent: Fiddler
Host: localhost:53393
Content-Length: 39
Content-Type: application/json

我的请求体

{
  "username":"",
  "password":""
}

路线

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

还有我 Controller 中的方法

[HttpPost]
public MethodResponse Authenticate(string username, string password)
{
    ConsoleServiceClient service = new ConsoleServiceClient();
    return service.Authenticate(username, password);
}

我使用的网址

http://localhost:53393/api/service/authenticate

我对此还是个新手,但不明白为什么不支持 POST?

谢谢

最佳答案

尝试使用 http://localhost:53393/api/service 作为您的 URI,因为目前您的 API 路由中没有 {action} 段。

关于c# - HTTPPost MVC 4 网络 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9700706/

相关文章:

c# - View 模型实现 - 字段少于实体

c# - 返回 HttpResponseMessage 时的 WebAPI Gzip

post - WebAPI - 属性路由 POST 不适用于 WebAPI Cors?

c# - 使用网络摄像头进行人脸检测

c# - 我如何在 C# 中加密 web.config 变量

c# - C#获取外部进程的线程起始地址

asp.net - 如何为 ASP.NET MVC Web 应用程序中使用的图像添加缓存?

c# - 只要支持.NET4,托管就支持MVC5吗?

VS2015 中的 Azure 应用服务项目模板和新发布目标

c# - ImageView 比例和宽度/高度变化