0
votes

I am learning webapi.I am running my webapi project and hitting the url localhost:54099/api/values

but Get is not executed.I am not able to see the data.Where i am doing wrong?When i hit the url,below popup shows but i am neither able to save it or open it. When i hit the same url in Chrome,i am getting the error

An error occurred when trying to create a controller of type 'ValuesController'. Make sure that the controller has a parameterless public constructor.

enter image description here

WebAPI method inside ValuesController

  public IEnumerable<Book> Get()
            {
                var Books = iLibrary.GetBooks();
                return Books.ToList();
            }

WebApiConfig

 public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
2
this problem occurs in ie only..test it on chrome or other browser - Kartikeya Khosla
If you want to use an external client app to test your API, you should familiarize yourself with something like Fiddler. Also consider writing unit and integration tests so that you can assert your expectations and not manually confirm them using an external app. - David Peden

2 Answers

1
votes

Your endpoint is correctly executed and seems to return the data. What you are seeing is Internet Explorer's default behavior for displaying the data you return (JSON or XML I assume), namely to download it as a file.

Use Chrome/Firefox for that. Based on whether you return JSON or XML you might also want to use plugins that format the response in your browser s.t. it is more readable. For instance

0
votes

I found the solution.I am using wrong dll to resolve dependency injection.I was using Unity.MVC4,but since its a webapi project,i have to use Unity.WebAPI.