2017-09-01 60 views
0

我應該輸入什麼URI才能運行我的Web API 2評分路線?我應該輸入什麼URI來運行我的Web API2?

我試圖用POST方法如下URI和我得到的404錯誤:

http://localhost:52229/PersonalAutoAPI/RunRating 

這一個工程(不同的控制器):

http://localhost:52229/PersonalAutoAPI/Drivers 

我可以讓我的司機API

這是我的webapiconfig.config.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web.Http; 

namespace PersonalAuto 
{ 
    public static class WebApiConfig 
    { 
     public static void Register(HttpConfiguration config) 
     { 
      // Web API configuration and services 

      // Web API routes 
      config.MapHttpAttributeRoutes(); 

      config.Routes.MapHttpRoute(
       name: "PersonalAutoAPI", 
       routeTemplate: "PersonalAutoAPI/{controller}/{id}", 
       defaults: new { id = RouteParameter.Optional } 
      ); 


      var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml"); 
      config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType); 
     } 
    } 
} 

這是我RatingControler.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Http; 
using PersonalAuto.Models; 

namespace PersonalAuto.Controllers 
{ 
    public class RatingControler : ApiController 

    { 
     public IEnumerable<RatingResult> PostRunRaing([FromBody] RatingInfo MyRateInfo) 
     { 
      RatingResult[] myRatingResult = 
       {new RatingResult{PremiumDP = 0M,PremiumEFTDownPament = 0M,PremiumMontlyPayment = 0M,PremiumEFTMonthlyPayment=0M,PremiumPIF=0M } 
      }; 
      return myRatingResult; 
     } 

     public IHttpActionResult PostRunRating([FromBody] RatingInfo MyRateInfo) 
     { 
      RatingResult MyRating = new RatingResult { PremiumDP = 0M, PremiumEFTDownPament = 0M, PremiumMontlyPayment = 0M, PremiumEFTMonthlyPayment = 0M, PremiumPIF = 0M }; 


      if (MyRating == null) 
      { 
       return NotFound(); 
      } 


      return Ok(MyRating); 

     } 
    } 
} 

這是我diverscontroler.cs(這一點也適用上述URI)

using PersonalAuto.Models; 
using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Web.Http; 
using System.Linq; 
using System.Net.Http; 
using System.IO; 

namespace PersonalAuto.Controllers 
{ 
    public class DriversController : ApiController 
    { 

     public IEnumerable<Driver> GetAllDrivers() 
     { 
      Driver[] myDriverArray = 
       { 
        new Driver { id = "1234", first_name = "eric", last_name = "last", dl_number = "1234", address_1 = "1234 test st", address_2 = "", city = "dallas", state = "TX", zip = "75248", mobile = "214-415-9224" } 
      }; 
      return myDriverArray; 
     } 

     public IHttpActionResult GetDriverByLicenseNo(string drivers_license_number, string drivers_license_state) 
     { 
      string PreToken = "xxxxx:xxx:xxxxxxxxxxxxx"; 
      string Token = Convert.ToBase64String(Encoding.UTF8.GetBytes(PreToken)); 

      if (drivers_license_state == null) 
       drivers_license_state = "TX"; 
      var driver = new Driver { id = Token, first_name = "eric", last_name = "last", dl_number = "1234", address_1 = "1234 test st", address_2 = "", city = "dallas", state = "TX", zip = "75248", mobile = "214-415-9224" }; // products.FirstOrDefault((p) => p.Id == id); 
      if (driver == null) 
      { 
       return NotFound(); 
      } 
      return Ok(driver); 

     } 

    } 
} 

我已經出來拉我的頭髮在這!任何幫助,將不勝感激!

+1

您的控制器被稱爲「Rating」而不是「RunRating」,正確的URI是「http:// localhost:52229/PersonalAutoAPI/Rating」。另外請注意,您目前有兩種映射到相同路由「PostRunRaing」和「PostRunRating」的操作方法,如果要將它們都保存在同一個控制器中,則需要更改其中一個路由。 –

+1

路由設置取決於以'Controller'結尾的控制器類名稱。在你的示例代碼中,你拼錯了:'RatingControler'應該是'RatingController' –

回答

0

路由設置取決於以Controller結尾的控制器類名稱。在你的示例代碼,您有它拼錯:RatingControler應該RatingController

同時檢查的PostRunRaing拼寫 - 這似乎應該是PostRunRating,(但不能是因爲你將有一個命名衝突。控制器不知道是否返回一個IEnumerable或IHttpActionResult)

解決這個問題,然後我想你的路線將是:

http://localhost:52229/PersonalAutoAPI/Rating/PostRunRating

0

有代碼有兩個問題:

  1. 將Rating RatingControler更改爲RatingController。控制器工廠不認可RatingControler,因爲它需要*控制器。
  2. 你不能有兩個方法具有相同的http動詞和方法名稱。你不能在控制器中重載方法。
相關問題