不遵守 Webapi v1 路由
Webapi v1 routes are not adhered to
webapi调用returns报错:
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}/")]
.....
您收到的错误消息与路由的配置方式有关,而不是 Ninject。 WebAPI(1.0 或 2.2)的良好做法是使用以下模板:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
与 MVC 不同,不包含操作名称。
所以,尝试使用设置的路由,看看是否可以正常工作。
@Karen_B 为我指明了正确的方向,但我想提供应用的更改以防其他人遇到此问题。
我将路由映射更改为:
config.Routes.MapHttpRoute(
name: "NewForm",
routeTemplate: "api/{controller}/form/",
defaults: new
{
controller = "Dmpe"
},
constraints: new { httpMethod = new HttpMethodConstraint(new[] { HttpMethod.Post }) }
);
config.Routes.MapHttpRoute(
name: "GetForm",
routeTemplate: "api/{controller}/form/{id}",
defaults: new
{
controller = "Dmpe",
id = RouteParameter.Optional
},
constraints: new { httpMethod = new HttpMethodConstraint(new[] { HttpMethod.Get }) }
);
然后将控制器更改为:
[HttpPost]
public HttpResponseMessage Post(DmpeForm form)
{
....
[HttpGet]
public HttpResponseMessage Get(long Id)
{
.....
将 http 方法属性添加到方法中可能是多余的,因为我更改了方法名称以反映 http 方法,但此时我只是想让它工作。如果可以,请使用 WebApi 2 :-)
webapi调用returns报错:
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}/")]
.....
您收到的错误消息与路由的配置方式有关,而不是 Ninject。 WebAPI(1.0 或 2.2)的良好做法是使用以下模板:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
与 MVC 不同,不包含操作名称。
所以,尝试使用设置的路由,看看是否可以正常工作。
@Karen_B 为我指明了正确的方向,但我想提供应用的更改以防其他人遇到此问题。
我将路由映射更改为:
config.Routes.MapHttpRoute(
name: "NewForm",
routeTemplate: "api/{controller}/form/",
defaults: new
{
controller = "Dmpe"
},
constraints: new { httpMethod = new HttpMethodConstraint(new[] { HttpMethod.Post }) }
);
config.Routes.MapHttpRoute(
name: "GetForm",
routeTemplate: "api/{controller}/form/{id}",
defaults: new
{
controller = "Dmpe",
id = RouteParameter.Optional
},
constraints: new { httpMethod = new HttpMethodConstraint(new[] { HttpMethod.Get }) }
);
然后将控制器更改为:
[HttpPost]
public HttpResponseMessage Post(DmpeForm form)
{
....
[HttpGet]
public HttpResponseMessage Get(long Id)
{
.....
将 http 方法属性添加到方法中可能是多余的,因为我更改了方法名称以反映 http 方法,但此时我只是想让它工作。如果可以,请使用 WebApi 2 :-)