NET
NET

Reputation: 289

mvc4 web api route sheme

I want access resource like this:

     /api/adddevice/12345

what is incorrect in this route?

    routes.MapRoute(
            name: "AddDevice",
            url: "adddevice/{id}",
            defaults: new { controller = "some controller", action = "adddevice", id = UrlParameter.Optional }
    );


    public string AddDevice(int id)
    {
        return "Device " + id.ToString() + " succesfully added";
    }

EDIT

Here is error details:

     <MessageDetail>
        No type was found that matches the controller named 'some controller'.
     </MessageDetail>

Upvotes: 0

Views: 153

Answers (2)

anaximander
anaximander

Reputation: 7140

You say you want the url to be this:

/api/adddevice/12345

but your route mapping has this:

url: "adddevice/{id}",

See how it doesn't match? The mapping doesn't have api/ at the start. The URL stated in the MapRoute() call has to match all of the URL (except the host part, of course). Either remove /api/ from the URL you're typing, or add it to the URL you're mapping.

Incidentally, routes.MapRoute() is for MVC; for WebApi you want config.Routes.MapHttpRoute().

Upvotes: 3

Jon Susiak
Jon Susiak

Reputation: 4978

You missed out the 'api' in the route and you appear to be using MVC routing.

Note this needs to be added to the WebApiConfig and not the RouteConfig:

config.Routes.MapHttpRoute(
                name: "AddDevice",
                routeTemplate: "api/adddevice/{id}",
                defaults: new { controller = "somecontroller", action = "adddevice", id = UrlParameter.Optional }
                );

Upvotes: 1

Related Questions