1

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?

4
  • When running locally the two sites will have different ports, make sure you're going to the correct one. Commented Aug 17, 2017 at 9:41
  • localhost:31003/api/statistics/topUsers try this Commented Aug 17, 2017 at 9:44
  • @ryan-searle how to define which port goes each site? Commented Aug 17, 2017 at 9:44
  • See here stackoverflow.com/questions/22822209/… Commented Aug 17, 2017 at 9:46

2 Answers 2

1

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

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

Comments

0

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/

Comments

Your Answer

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