mbrc
mbrc

Reputation: 3953

WebApi2 can not find route

[RoutePrefix("subscriptions")]
    public class SubscriptionsController : ApiController
    {
        private SubscriptionsService _subscriptionsService;

        public SubscriptionsController()
        {
            _subscriptionsService = new SubscriptionsService();
        }

        [Route("{email}")]
        [HttpGet]
        public IHttpActionResult Get(string email)
        {

but when I try

http://localhost:51561/api/subscriptions/myEmail

No HTTP resource was found that matches the request URI

'http://localhost:51561/api/subscriptions/myEmail'.

any idea why?

I also set everything in 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 }
            );
        }
    }

Upvotes: 1

Views: 55

Answers (2)

Hiran
Hiran

Reputation: 1180

You have tried a wrong URL. Correct URL should be,

http://localhost:51561/subscriptions/myEmail

OR

If you need API prefix, then you need to change the route prefix of the controller as below,

[RoutePrefix("API/subscriptions")]

then this URL will work,

http://localhost:51561/api/subscriptions/myEmail

Note that , here you are using two routing, default routing and attribute routing. if you need to continue with attribute routing , you can comment out the default routing.

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

Upvotes: 1

Piotr
Piotr

Reputation: 1177

I Suppose you are creating some webApi service ? Did you register this route in global.asax.cs ? Something like this :

        httpConfiguration.Routes.MapHttpRoute(
            name: "MyWebApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { controller = "ControllerName", action = "ActionName", id = RouteParameter.Optional },
            constraints: null
        );

Upvotes: 0

Related Questions