Question

I am creating an API wrapper for some old stored procedures alongside a new MVC 4 app. So far I have created this class:

public class Event
{
    public int ID;
    public DateTime DateBegin;
    public DateTime DateEnd;
    public DateTime TimeBegin;
    public int Duration;
    public string Name;
    public string Description;

    public static  IEnumerable<Event> GetEventSummary()  //List<Event> GetEventSummary()
    {
        List<Event> events = new List<Event>();
        DataTable thisDT = new DataTable();
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
        {
            string SprocName = "HE_GetEventSummary";
            SqlDataAdapter thisAdapter = new SqlDataAdapter(SprocName, connection);
            thisAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            thisAdapter.Fill(thisDT);
        }

        foreach(DataRow row in thisDT.Rows)
        {
            Event myEvent = new Event();
            myEvent.ID = Convert.ToInt32(row["EventID"]);
            myEvent.DateBegin = Convert.ToDateTime(row["EventDateBegin"].ToString());
            myEvent.DateEnd = Convert.ToDateTime(row["EventDateEnd"]);
            myEvent.TimeBegin = Convert.ToDateTime(row["EventTimeBegin"]);
            myEvent.Duration = Convert.ToInt32(row["Duration"]);
            myEvent.Name = row["EventName"].ToString();
            myEvent.Description = row["EventDesc"].ToString();

            events.Add(myEvent);
        }

        return (IEnumerable<Event>)events;
    }
}

Then I setup this empty API controller from the templates provided in Visual Studio:

public class EventController : ApiController
{
    // GET api/event
    public IEnumerable<HobbsEventsMobile.Models.Event> Get()
    {
        return HobbsEventsMobile.Models.Event.GetEventSummary();
        //return new string[] { "value1", "value2" };
    }

    // GET api/event/5
    public string Get(int id)
    {
        return "value";
    }

    // POST api/event
    public void Post([FromBody]string value)
    {
    }

    // PUT api/event/5
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api/event/5
    public void Delete(int id)
    {
    }
}

My Routes look like this:

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

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

and the api routes:

    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { action = "Get" }
        );
    }

But when I visit http://localhost:60009/api/event (as specified by the comment in the api controller), I get this error:

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /api/event

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34009

What am I doing wrong?

Was it helpful?

Solution

We have to register Web API Routes in Global.asax in following way. Or else Web API routes will not be available to resolve requests.

GlobalConfiguration.Configure(WebApiConfig.Register);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top