简体   繁体   中英

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

I got an error when trying to test my web api url

This is my 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 }
            );
        }

Here is my controller:

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

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

This is the error message that i got:

{
    "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'"
}

This is my url that i tested:

http://localhost:49484/api/test

You have not mentioned action in routing. Remove [Route("test")] .

Try below code.

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

If you want to add route path on action level use MapHttpAttributeRoutes();

You need to have action in your routeTemplate. You can do this:

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

and test it like:

http://localhost:49484/api/test

Or you can also do

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

and test it like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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