我应该输入什么URI来运行我的Web API2?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我应该输入什么URI来运行我的Web API2?相关的知识,希望对你有一定的参考价值。
我应该输入什么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);
}
}
}
我一直把头发拉过来!任何帮助,将不胜感激!
答案
路由设置取决于以Controller结尾的控制器类名。在你的示例代码中,你拼错了:RatingControler
应该是RatingController
还要检查PostRunRaing
的拼写 - 看起来它应该是PostRunRating
,(但它不能因为那样你会有命名冲突。控制器不知道是否返回IEnumerable或IHttpActionResult。)
解决这个问题,然后我认为您的路线将是:
http://localhost:52229/PersonalAutoAPI/Rating/PostRunRating
另一答案
代码有两个问题:
- 将RatingControler更改为RatingController。控制器工厂不识别RatingControler,因为它需要* Controller。
- 你不能有两个方法具有相同的http动词和方法名称。你不能在控制器中重载方法。
以上是关于我应该输入什么URI来运行我的Web API2?的主要内容,如果未能解决你的问题,请参考以下文章