1
votes

i am new in asp.net web api. just reading a article on web api from this url http://www.c-sharpcorner.com/article/remote-bind-kendo-grid-using-angular-js-and-Asp-Net-web-api/

enter image description here

now see this code

[RoutePrefix("api/EmployeeList")]  
public class EmployeeDetailsController : ApiController  
{  
    [HttpGet]  
    [Route("List")]  
    public HttpResponseMessage EmployeeList()  
    {  
        try  
        {  
            List<Employee> _emp = new List<Employee>();  
            _emp.Add(new Employee(1, "Bobb", "Ross"));  
            _emp.Add(new Employee(2, "Pradeep", "Raj"));  
            _emp.Add(new Employee(3, "Arun", "Kumar"));  
            return Request.CreateResponse(HttpStatusCode.OK, _emp, Configuration.Formatters.JsonFormatter);  
        }  
        catch (Exception ex)  
        {  
            return Request.CreateResponse(HttpStatusCode.OK, ex.Message, Configuration.Formatters.JsonFormatter);  

        }  


    }  
}  

as per my understanding the requesting url should be /api/EmployeeList/List but if anyone look the above image then must notice different url api/Employee/GetEmployeeList is being used to call list action method. so i just like to know the reason why different url is getting issued to call List action function ?

also do not understand how this url api/Employee/GetEmployeeList can work in this situation because controller name is EmployeeDetailsController but RoutePrefix has been used to address it api/EmployeeList and action method name is EmployeeList() which has been change to List..........so some one tell me how this url api/Employee/GetEmployeeList can invoke list action of web api ?

please discuss in detail. thanks

3
controller name is EmployeeDetails then how could i point to this controller with keyword like Employee....i have confusion here? - Monojit Sarkar
action name is EmployeeList then how could i access it like GetEmployeeList ? - Monojit Sarkar
Have you configured attribute routing? it maybe not configured and thus convention based routes are used instead, for example if HttpGet is used then Get is prefix to the action name and thus GetEmployeeList. is is recommended that you read more about routing in web api 2, here is a link asp.net/web-api/overview/web-api-routing-and-actions/… - RaniDevpr
Your code and your POSTMAN response do not match, the user names returned are different. You are forgetting to tell us something. Do your application compile in Visual Studio? Are you calling the correct endpoint? Are you sure you have not another Controller named Employee? Maybe you renamed your controller and didn't recompile. - Federico Dipuma
if you look at my post then must realize that i said i am learning web api and i also mention url from where i was reading a code for web api sample. i have confusion about endpoint url issued in post man and that is why i asked why the author use different url in postman? - Monojit Sarkar

3 Answers

0
votes

Did you activate AttributeRouting? If not, standard routing is in place and your current attributes will be ignored.

You need to do this in the WebApi registration process, like this:

    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

        // Other Web API configuration not shown.
    }

Then, you can remove any method call like this:

config.Routes.MapHttpRoute

to disable conventional routing.

BTW, the call api/Employee/GetEmployeeList is valid because Employee is the name of your controller, Get is the verb and EmployeeList is the name of the method.

0
votes

Did you enable attribute routing? You do this by default in webapiconfig.cs by adding this line of code:

config.MapHttpAttributeRoutes();

0
votes

@Monojit-Sarkar. The URL the user showed in postman did not also match the code. In the code, these are the users we expect to see:

        _emp.Add(new Employee(1, "Bobb", "Ross"));  
        _emp.Add(new Employee(2, "Pradeep", "Raj"));  
        _emp.Add(new Employee(3, "Arun", "Kumar"));  

But the results in postman are different as shown in the image the user posted. So something is disconnected from the article/image and the sample code.