Someone To Know Someone To Know - 6 months ago 36
ASP.NET (C#) Question

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?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download