【问题标题】:No HTTP resource was found that matches the request URI, I need solutions未找到与请求 URI 匹配的 HTTP 资源,我需要解决方案
【发布时间】:2019-08-05 17:25:53
【问题描述】:

我在尝试测试我的 web api url 时出错

这是我的 global.asax:

void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.MapHttpRoute(
               name: "DefaultApi",
               routeTemplate: "api/{controller}/{id}",
               defaults: new { id = System.Web.Http.RouteParameter.Optional }
            );
        }

这是我的控制器:

public class TestController : ApiController
    {
        [Route("test")]
        [HttpPost]
        public PaymentResponseModel Response()
        {
            log.Info("Hello world");

            PaymentResponseModel prm = new PaymentResponseModel();
            prm.info1 = "tes";
            return prm;
        }
     }

这是我收到的错误消息:

{
    "Message": "No HTTP resource was found that matches the request URI 'http://localhost:49484/api/test'.",
    "MessageDetail": "No route providing a controller name was found to match request URI 'http://localhost:49484/api/test'"
}

这是我测试的网址:

http://localhost:49484/api/test

【问题讨论】:

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


【解决方案1】:

您没有提到路由中的操作。删除[Route("test")]

试试下面的代码。

void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.MapHttpRoute(
           name: "DefaultApi",
           routeTemplate: "api/{controller}/{id}",
           defaults: new { action = "Response", 
                           id = System.Web.Http.RouteParameter.Optional }
        );
    }

如果您想在操作级别添加route path,请使用MapHttpAttributeRoutes();

【讨论】:

    【解决方案2】:

    您需要在您的 routeTemplate 中执行操作。你可以这样做:

    void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.MapHttpRoute(
           name: "DefaultApi",
           routeTemplate: "api/{controller}/{action}/{id}",
           defaults: new { action = "Response", 
                           id = System.Web.Http.RouteParameter.Optional }
            );
     }
    

    并像这样测试它:

    http://localhost:49484/api/test
    

    或者你也可以这样做

    void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.MapHttpRoute(
           name: "DefaultApi",
           routeTemplate: "api/{controller}/{action}/{id}",
           defaults: new { id = System.Web.Http.RouteParameter.Optional }
            );
     }
    

    并像这样测试它:

    http://localhost:49484/api/test/Response
    

    【讨论】:

      猜你喜欢
      • 2017-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      • 2020-08-24
      • 2016-12-01
      • 2016-01-26
      相关资源
      最近更新 更多