0

here is the startup ruinning controller method in web application:

public ActionResult PaginationOfBooks(string _title, string _author, string _description, string _publisher) { HttpClient client = new HttpClient(); client.BaseAddress = new Uri("http://localhost:57752"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.GetAsync("api/Book/GetBooks").Result; 

rest is not important yet. Actually at this point, in debug mode I expect to switch WebApi project that in same solution with Web app, GetBooks method I have break point at first line of this method.. here is the content of result:

StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: 

my web abi project:

public ActionResult GetBooks(string _title, string _author, string _description, string _publisher) { var draw = 1; ... 

webapi config:

public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API configuration and services // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/Book/{id}", defaults: new { id = RouteParameter.Optional } ); config.Formatters.Remove(config.Formatters.XmlFormatter); config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json")); } } 

how can I get this web api request?

I run api project and simply tried to access method with this: enter image description here

1

2 Answers 2

2

Add a route attribute before the api method

[Route("api/Book/GetBooks/{_title}/{_author}/{_description}/{_publisher}")] public ActionResult GetBooks(string _title, string _author, string _description, string _publisher) { var draw = 1; ... 

then call it as

public ActionResult PaginationOfBooks(string _title, string _author, string _description, string _publisher) { HttpClient client = new HttpClient(); client.BaseAddress = new Uri("http://localhost:57752"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.GetAsync("api/Book/GetBooks/" + _title + "/" + _author + "/" + _description + "/" + _publisher).Result; 
Sign up to request clarification or add additional context in comments.

6 Comments

its for GET not for POST, you used get so I show get
yes I am able to access api method with "localhost:57752/api/Book/GetBooks/title..." from browser but I need access it within application
I have show you how to access it using HttpResponseMessage response = client.GetAsync("api/Book/GetBooks/" + _title + "/" + _author + "/" + _description + "/" + _publisher).Result; , do you need more detail ?
No I am ok with it now its working :) but how can I a step into web api method in debugging, I simply press f11 in "client.GetAsync(..)" method and expected to switch the breakpoint in api method.. what should I do for debugging api ?
because its async method so you have to await it then you can set a breakpoint on it, try to apply async await approach
|
1

Change your WebApiConfig like this-

 config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); config.Routes.MapHttpRoute( name: "BooksApi", routeTemplate: "api/{controller}/{email}/{firstname}/{lastname}/{source}" ); 

And call from PaginationOfBooks like this-

HttpResponseMessage response = client.GetAsync("api/Book/GetBooks/" + _title + "/" + _author + "/" + _description + "/" + _publisher).Result; 

See if this helps.

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.