user8393506
user8393506

Reputation:

ASP MVC 4 + Web API

I have ASP MVC 4 project and the Web API.

structure

I wanna use Web API from the main application. i did this:

WebAPI Project

WebApiConfig.cs

public static void Register(HttpConfiguration config) {
            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            config.Formatters.JsonFormatter.SupportedMediaTypes
                                           .Add(new MediaTypeHeaderValue("text/html"));
}

Global.asax

protected void Application_Start() {
            GlobalConfiguration.Configure(WebApiConfig.Register);
}

StatisticsController.cs

public class StatisticsController : ApiController {
        TopUserFactory topUserFactory = new TopUserFactory();
        // GET api/statistics/topUsers
        [ActionName("topUsers")]
        public List<TopUser> Get() {
            return topUserFactory.Top10Users();
        }
}

But nothing happens when i go for localhost:31003/api/statistics/{topUsers}

How to use WebAPI project from other project?

Upvotes: 1

Views: 86

Answers (2)

Ryan Searle
Ryan Searle

Reputation: 1627

When working with multiple sites locally they will have different port numbers.

You can check the port numbers by clicking the IIS Express icon on your taskbar:

enter image description here

You can change the port number by adding a configuration:

Changing project port number in Visual Studio 2013

Upvotes: 1

Ruslan
Ruslan

Reputation: 10147

your code looks ok. it's very easy to get the routes wrong with WebAPI, ensure you're doing a parameter-less GET to http://localhost:31003/api/statistics/topUsers

failing that, use this tool: https://www.nuget.org/packages/routedebugger/

Upvotes: 0

Related Questions