0

I am trying to learn Web APi in asp.net by this tutorial : http://blogs.msdn.com/b/henrikn/archive/2012/02/23/using-asp-net-web-api-with-asp-net-web-forms.aspx

I have a website that is working fine , Now i want to integrate WEB API .I created simple WEB API Controller class "Controller" , put RouteTable.Routes.MapHttpRoute in Application_Start() in global.asax and then tried to call Api, but i am not getting data as output ..

this is controller class

public class Controller : ApiController
{
    // GET api/<controller>
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

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

    // POST api/<controller>
    public void Post([FromBody]string value)
    {
    }

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

    // DELETE api/<controller>/5
    public void Delete(int id)
    {
    }
}

Glogal Asax file:

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
         RouteTable.Routes.MapHttpRoute(
           name: "DefaultApi",
             routeTemplate: "api/{controller}/{id}",
             defaults: new { id = System.Web.Http.RouteParameter.Optional }
         );

    }

    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }

    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }

</script>

and trying to call

http://localhost:56497/api/values/ http://localhost:56497/api/

but it gives error :

<Error>
<Message>
No HTTP resource was found that matches the request URI 'http://localhost:56497/api/controller/'.
</Message>
<MessageDetail>
No type was found that matches the controller named 'controller'.
</MessageDetail>
</Error>

What is something that I am missing ?

1 Answer 1

1

Controller name for controller is not the best choice. Convention is that controllers should have names like SomethingController. Since you are calling it with /api/values, I think you should have named it ValuesController:

public class ValuesController : ApiController

By the way it looks like the tutorial you referenced uses exactly the same name.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.