TheBoubou
TheBoubou

Reputation: 19903

WebApi 2.0 - No HTTP resource was found that matches the request URI

With the code below I hit the constructor but never the action. The URL is http://localhost:64704/api/mytest/mymethod. Same problem when I tried to change the routing like this :

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}"
        );

[Route("api/[controller]")]
public class MyTestController : ApiController
{
    readonly IMyFacade myFacade;
    public MyTestController(IMyFacade myFacade) => this.myFacade = myFacade;

    [HttpPost]
    [Route("mymethod")]
    public IHttpActionResult MyMethodName([FromBody]MyModel model)
    {
        //some code here

        return Ok();
    }
}

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

Update 1

public class WebApiApplication : HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
        UnityConfig.RegisterComponents();
    }
}

Update 2

        public static void RegisterComponents()
    {
        var container = new UnityContainer();

        // register all your components with the container here
        // it is NOT necessary to register your controllers

        // e.g. container.RegisterType<ITestService, TestService>();

        /* ...... */

        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
    }

Update 3 { "Field1": "1", "Field2": "some key", "Field3": "some name of course" }

Upvotes: 1

Views: 12695

Answers (4)

Advait Baxi
Advait Baxi

Reputation: 1628

In controller, you need to specify RoutePrefix attribute instead of Route as shown in below code

// Specify 'RoutePrefix' attribute instead of 'Route'
[RoutePrefix("api/[controller]")]
public class MyTestController : BaseController
{
    [HttpPost]
    [Route("mymethod")]
    public HttpResponseMessage MyMethodName([FromBody]MyModel model)
    {
        return Request.CreateResponse(System.Net.HttpStatusCode.OK);
    }
}

and try again.

Upvotes: 0

Saineshwar Bageri - MVP
Saineshwar Bageri - MVP

Reputation: 4031

WebApiConfig

public static class WebApiConfig
{
    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 }
        );
    }
}

Global.asax

 public class WebApiApplication : System.Web.HttpApplication
 {
     protected void Application_Start()
     {
         AreaRegistration.RegisterAllAreas();
         GlobalConfiguration.Configure(WebApiConfig.Register);
         FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
         RouteConfig.RegisterRoutes(RouteTable.Routes);
         BundleConfig.RegisterBundles(BundleTable.Bundles);
     }
 }

Controller

public class MyTestController : ApiController
{      
    [HttpPost]
    [Route("mymethod")]
    public IHttpActionResult MyMethodName([FromBody]MyModel model)
    {
        return Ok();
    }
}

While Debugging

enter image description here

Sending PostRequest

enter image description here

Upvotes: 1

Marcus H&#246;glund
Marcus H&#246;glund

Reputation: 16801

The issue occurs because you are expecting that the Route attribute ontop of the controller will be merge with the Route attribute ontop of the method. This is not the case in .net WebApi 2.0, (added in .net core mvc)

You should instead use the RoutePrefix attribute on the controller to make the controller route to be merged with the method routes

[RoutePrefix("api/[controller]")]
public class MyTestController : ApiController
{
    readonly IMyFacade myFacade;
    public MyTestController(IMyFacade myFacade) => this.myFacade = myFacade;

    [HttpPost]
    [Route("mymethod")]
    public IHttpActionResult MyMethodName([FromBody]MyModel model)
    {
        //some code here

        return Ok();
    }
}

Upvotes: 3

Sunil Shrestha
Sunil Shrestha

Reputation: 313

You are using HttpPost verbs in action, make sure you use HttpPost method when sending request.

Upvotes: -1

Related Questions