1

I'm new to API designing with VS2017 and I'm trying to make my simple API work with few SQL objects in a DB.

I have a fairly simple project which looks like this :

WebApiConfig.cs :

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

Which I believe is stock so should get me where I want to. I have some controllers based on the same principles, here's one for example :

    public class UsersController : Controller
    {
    private APIContext db = new APIContext();

    // GET: Users
    public ActionResult Index()
    {
        return View(db.Users.ToList());
    }

    // GET: Users/Details/5
    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Users users = db.Users.Find(id);
        if (users == null)
        {
            return HttpNotFound();
        }
        return View(users);
    }
  }

There are more of them, but everything has been autogenerated so I don't think I have to show them.

The problem is that when I get to localhost/api/users, I get the 404 error page : No HTTP resource was found that matches the request URI 'http://localhost:myport/api/users'.

Same thing when I'm trying to access a specific id with /api/users/1 Can anyone point me where I should try and change things ? I'm lost in the jungle of the config files and routes !

Thanks !

EDIT : After some good answers, here's some more information: I'm wondering if the issue is not somewhere else. When I'm on the localhost/api, I get a "beautiful" error page but when I try to access the /api/users/index I get an XML response with a 404 message in it. Is that a sign of another problem ? Something to note is that the Swagger UI shows absolutely nothing.

3 Answers 3

2

Your Routing configuration mentions only a Controller in the template without a default Action associated with.

Multiple choices are available to you, however, I would suggest to go for a simple one as in:

public static void Register(HttpConfiguration config)
{
    // Web API configuration and services

    // Web API routes
    //config.MapHttpAttributeRoutes();

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

Now the Action is part of the template, with a default value of index, you will have the following:

http://localhost:myport/api/users redirecting to UsersController.Index
http://localhost:myport/api/users/index redirecting to UsersController.Index
http://localhost:myport/api/users/details redirecting to UsersController.Details
http://localhost:myport/api/users/details/123 redirecting to UsersController.Details

Edit After a second investigation, it appears that you are using an MVC Controller rather than a WebApi Controller. While they both have the same name, they belong to different namespaces and need their own config.

In order to configure your MVC controller route, ensure to have a class as follow in your App_Start folder:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

Then, from the Global.asax, in Application_Start method, ensure to have the following call:

RouteConfig.RegisterRoutes(RouteTable.Routes);

as in:

protected void Application_Start()
{
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    GlobalConfiguration.Configure(WebApiConfig.Register);
}

From this point, you can now access your controller via http://localhost:myport/users.

On the other end, if you want to do an API returning data rather than views, you would need your controller to inherit from ApiController.

Sign up to request clarification or add additional context in comments.

4 Comments

put a comma between action = "index" and id = RouteParameter.Optional
Thanks, didn't see it at first.
Hi, thank you for the interest, your answer doesnt seem to solve my problem. I'm wondering if the issue is not somewhere else. When I'm on the localhost/api, I get a "beautiful" error page but when I try to access the /api/users/index I get an XML response with a 404 message in it. Is that a sign of another problem ? Thanks again.
@AzirisMorora It appears you are using an MVC controller rather than an API controller. In order to access it, you need to make sure you are configuring the MVC routes rather than the WebApi routes. The edit made to the answer should lead you to the proper path.
1

Use http://localhost:yourport/users/index" instead.

The URL format is always Controller/Action/Parameters.

1 Comment

Thanks, as I updated, the error page is not the same when I try to access the "good" routes. But my issue is not solved yet.
1

Add a new route with {action}

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

URL: http://localhost:myport/api/users/details/123456

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.