【发布时间】:2023-11-29 06:46:01
【问题描述】:
webapi调用返回错误:
message: "No HTTP resource was found that matches the request URI 'http://localhost:25396/api/dmpe/form/123'.",
messageDetail: "No action was found on the controller 'Dmpe' that matches the request."
控制器:
[HttpGet]
[ActionName("GetForm")]
public HttpResponseMessage GetForm(long formId)
{
try
{
var form = this.dmpeService.GetForm(formId);
return Request.CreateResponse(HttpStatusCode.OK, form);
}
catch (Exception e)
{
var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(new { success = false, message = e.Message });
_logger.Error("GetForm Error: {0}", e.Message);
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, json);
}
}
WebApi 路由配置:
//config.Routes.MapHttpRoute(
// name: "GetForm",
// routeTemplate: "api/dmpe/form/{id}",
// defaults: new
// {
// controller = "Dmpe",
// action = "GetForm"
// }
// );
更新路线:
config.Routes.MapHttpRoute(
name: "GetForm",
routeTemplate: "api/{controller}/form/{id}"
);
我的路线基于当前的命名约定。这条路线有效。
config.Routes.MapHttpRoute(
name: "GetMenu",
routeTemplate: "api/forms/menu/{userTypeId}/{ctyhocn}",
defaults: new
{
controller = "Service",
action = "GetMenu"
},
constraints: new { httpMethod = new HttpMethodConstraint(new[] { HttpMethod.Get.ToString() }) }
);
现在在 WebApi v2 中,我已经习惯于在控制器中定义路由,如下所示,但这是我正在寻找的 v2 功能。根据当前的示例,我会认为 routeTemplate 会起作用。我查看了我定义的路由的 RouteTables.Routes,它的格式似乎正确:“api/{controller}/form/{id}”
WebApi v2 路由属性
[HttpGet]
[AllowAnonymous]
[Route("api/office/companynames/{searchValue}/")]
.....
【问题讨论】:
标签: asp.net-mvc-4 asp.net-web-api ninject