我应该输入什么URI来运行我的Web API 2评级路由?
我使用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);
}
}
}
我一直把头发拉过来!任何帮助将不胜感激!
答案 0 :(得分:0)
路由设置取决于以Controller结尾的控制器类名。在示例代码中,您拼错了:RatingControler
应为RatingController
同时检查PostRunRaing
的拼写 - 看起来应该是PostRunRating
,(但不能因为那时你会有命名冲突。控制器不知道是否要返回IEnumerable或IHttpActionResult。)
修复此问题,然后我认为您的路线将是:
答案 1 :(得分:0)
代码有两个问题: