Working with HttpGet in asp.net Web API


In this article we will focus on how to give a different name to a GET method in a ASP.NET Web API project. We will also see how to resolve the error:HTTP/1.1 405 Method Not Allowed when accessing the Get method of a controller.

In this article we will focus on how to give a different name to a GET method in a ASP.NET Web API project. We will also see how to resolve the error:HTTP/1.1 405 Method Not Allowed.

As we have seen in the previous article how we can call GET method using the url:
http://localhost:port#/api/Student

This uri calls the GET method from the StudentController class of our project.

Now my requirement is that my method name should not start with Get verb but still I should be able to get all the list of students. So renamed the method to StudentList as shown below


public List StudentList()
{
List lst = new List();
lst.Add(new Student(1, "john", new DateTime(2014, 1, 1), "B.Tech I year"));
lst.Add(new Student(1, "robert", new DateTime(2014, 2, 1), "B.Tech I year"));
lst.Add(new Student(1, "tina", new DateTime(2014, 1, 4), "B.Tech I year"));
return lst;
}


When I try to use the same above url I got the error: 405 Method Not Allowed. We got this error because the default web api routing template uses the HTTP method(GET) to select the action. Therefore it was trying to find the method with the name "GET". So how do I call the "StudentList" method with the Get verb. In order to resolve this we need to annotate our method with the HttpGet attribute as shown below:

[HttpGet]
public List StudentList()
{
List lst = new List();
lst.Add(new Student(1, "john", new DateTime(2014, 1, 1), "B.Tech I year"));
lst.Add(new Student(1, "robert", new DateTime(2014, 2, 1), "B.Tech I year"));
lst.Add(new Student(1, "tina", new DateTime(2014, 1, 4), "B.Tech I year"));
return lst;
}


Now try accessing the same url and you will get the output as list of students in json format.

Now we will see how to give a different name to a web api method instead of Get verb.
1.We have to use ActionName attribute on top of the method definition which will be used as the name of the action.

[HttpGet]
[ActionName("StudentList")]
public List StudentList()
{
List lst = new List();
lst.Add(new Student(1, "john", new DateTime(2014, 1, 1), "B.Tech I year"));
lst.Add(new Student(1, "robert", new DateTime(2014, 2, 1), "B.Tech I year"));
lst.Add(new Student(1, "tina", new DateTime(2014, 1, 4), "B.Tech I year"));
return lst;
}

2.In the Register method of the WebApiConfig file add the below statement as the first statement.
config.Routes.MapHttpRoute(name: "ActionApi1", routeTemplate: "api/{controller}/{action}");

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

}
}

It indicates that a GET request for "api/Student/StudentList" would map to the StudentList method. Here Student is for StudentController and StudentList is the action method name.

Now the request for the uri:http://localhost:6329/api/Student/StudentList will call the StudentList method of StudentController class.


Comments

Author: Ritesh13 Mar 2014 Member Level: Bronze   Points : 0

Nice post..



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: