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

TyForHelpDude
  • 4,828
  • 10
  • 48
  • 96
  • Possible duplicate of [Attribute routing with optional parameters in ASP.NET Web API](http://stackoverflow.com/questions/22388452/attribute-routing-with-optional-parameters-in-asp-net-web-api) – Eris Jul 24 '16 at 16:28

2 Answers2

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;
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
  • its for GET not for POST, you used get so I show get – Mostafiz Jul 24 '16 at 16:40
  • yes I am able to access api method with "localhost:57752/api/Book/GetBooks/title..." from browser but I need access it within application – TyForHelpDude Jul 24 '16 at 16:43
  • 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 ? – Mostafiz Jul 24 '16 at 16:49
  • 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 ? – TyForHelpDude Jul 24 '16 at 16:51
  • because its async method so you have to await it then you can set a breakpoint on it, try to apply async await approach – Mostafiz Jul 24 '16 at 16:55
  • How can I convert this request to await, is it easy or some extra work ? – TyForHelpDude Jul 24 '16 at 17:26
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.

Sanket
  • 19,295
  • 10
  • 71
  • 82