c# - 路线在邮递员中无法正常工作,因此不允许发布
问题描述
我正在使用 mvc 和 Visual Studio 创建一个 Web api,但是我遇到了一个问题。我通过本地主机而不是浏览器的方式在 post man 的桌面版本上运行它。
我调用网址的方式是
http://localhost:12513/api/CustomerContracts/AddCustomer
[System.Web.Http.HttpPost]
[System.Web.Http.Route("AddCustomer/{description}/{customerRef}/{contractTypeId}/{startDate}/{EndDate}")]
public async Task AddCustomer(string Description, string CustomerRef, int ContractTypeId, DateTime startDate, DateTime endDate)
{
CustomerContracts _newContact = new CustomerContracts();
try
{
_newContact.Description = Description;
_newContact.CustomerRef = CustomerRef;
_newContact.ContractTypeId = ContractTypeId;
_newContact.StartDate = startDate;
_newContact.EndDate = endDate;
db.Entry(_newContact).State = EntityState.Modified;
db.CustomerContract.Add(_newContact);
await db.SaveChangesAsync();
}
catch (Exception x)
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(x.Message) });
}
}
当我在邮递员中运行上述内容时,它给了我以下错误
{ "Message": "请求的资源不支持 http 方法 'POST'。" }
我在同一个问题上尝试了另一个 SO,但它没有解决我的问题。
我使用的是第一次创建时位于 web api 项目中的默认路由文件。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
编辑 2
我提出了以下建议以及邮递员中错误消息的相同结果。
[System.Web.Http.HttpPost]
[System.Web.Http.Route("AddCustomer/{Description}/
{CustomerRef}/{ContractTypeId}/{startDate}/{endDate}")]
public async Task AddCustomer(string Description, string CustomerRef, int ContractTypeId, DateTime startDate, DateTime endDate)
{
CustomerContracts _newContact = new CustomerContracts();
try
{
_newContact.Description = Description;
_newContact.CustomerRef = CustomerRef;
_newContact.ContractTypeId = ContractTypeId;
_newContact.StartDate = startDate;
_newContact.EndDate = endDate;
db.Entry(_newContact).State = EntityState.Modified;
db.CustomerContract.Add(_newContact);
await db.SaveChangesAsync();
}
catch (Exception x)
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(x.Message) });
}
}
解决方案
必须与您编写列表名称的routing attributes
方式完全相同。parameters
所以而不是:
[System.Web.Http.Route("AddCustomer/{description}/{customerRef}/{contractTypeId}/{startDate}/{EndDate}")]
它应该是
[System.Web.Http.Route("AddCustomer/{Description}/{CustomerRef}/{ContractTypeId}/{startDate}/{endDate}")]
请注意我如何更改路径属性以匹配下面的参数列表。
public async Task AddCustomer(string Description, string CustomerRef, int ContractTypeId, DateTime startDate, DateTime endDate)
{
}
第二:你需要WebApiConfig.cs
添加App_Start
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/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
然后:在Global.asax
file inApplication_Start
方法中,添加以下行来注册WebApi Router
GlobalConfiguration.Configure(WebApiConfig.Register);
推荐阅读
- r - 分类树的 R 问题
- apache-spark - 如何限制 Jupyter Notebook 的 PySpark 集群上的资源?
- android - 为什么相机不颤抖打开?
- visual-studio - VS Debugger 是否有任何脚本支持,类似于 GDB 的脚本?
- javascript - 在js或jquery中点击广告时调用函数
- php - 如何在一列php中对子数组进行分组
- php - 如何通过 PHP 生成 {0} json 字符串?
- javascript - JS React 导入差异包大小
- javascript - Sensortower daterangepicker 设置期间的最小值最大值
- c++ - 在 C++20 中还需要 std::ssize() 吗?