Reputation: 2478
I'm trying to get a test application running using MVC WEB API but i can't get it work. What i want to do is send a GET request and as an answer get log records from a database as response.
Here's what i've set up:
Global.asax.cs: (unmodified)
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
my controller is called DBModelController:
public class DBModelController : ApiController
{
public IEnumerable<Log> GetAllLogs()
{
IDBModel DAO = new DBModelDAO();
IList<Log> Logs = DAO.GetLogs(DateTime.Now, DateTime.Now); //this gives back Log objects
return Logs;
}
}
and how i want it to use:
http://localhost:15339/api/logs --and in return i get back a set of serialized records
thank you very much in advance
Upvotes: 1
Views: 450
Reputation: 32758
Create a new route if you are not interested to change the controller class name into Logs
.
routes.MapHttpRoute(
name: "log_route",
routeTemplate: "api/logs",
defaults: new { controller = "DBModel", action = "GetAllLogs" }
);
This route should be placed before the default routes.
Upvotes: 1