2016-07-24 101 views
0

這裏找到API方法傾斜度爲啓動ruinning控制器方法在Web應用程序:網絡從Web應用程序

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; 

休息並不重要呢。其實在這一點上,在調試模式下我期待切換,在使用Web應用程序相同的解決方案,GetBooks方法我在這個方法的第一線破發點的WebAPI項目..這裏是結果的內容:

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

我的網絡ABI項目:

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

的WebAPI配置:

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")); 
     } 
    } 

我怎麼能得到這個網絡API請求?

我跑API項目,只是試圖用這種訪問方法: enter image description here

+0

[ASP.NET Web API中可選參數的屬性路由]的可能重複(http://stackoverflow.com/questions/22388452/attribute-routing-with-optional-parameters-in-asp-net-web- api) – Eris

回答

2

API方法前加一個路由屬性

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

然後把它作爲

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; 
+0

ty for post但仍然是相同的。 – TyForHelpDude

+0

它的GET不適用於POST,您使用得到所以我顯示得到 – Mostafiz

+0

是的我能夠訪問api方法與「localhost:57752/api/Book/GetBooks /標題.. 。「來自瀏覽器,但我需要在應用程序內訪問它 – TyForHelpDude

1

變化你的WebApiConfig像這樣 -

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}" 
    ); 
0123從PaginationOfBooks

,並呼籲像這個 -

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

看看這會有所幫助。