繁体   English   中英

未找到与请求 URI 匹配的 HTTP 资源,我需要解决方案

[英]No HTTP resource was found that matches the request URI, I need solutions

尝试测试我的 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

您没有提到路由中的操作。 删除[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();

您需要在您的 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM