[英]No HTTP resource was found that matches the request URI, I need solutions
I got an error when trying to test my web api url尝试测试我的 web api url 时出错
This is my global.asax:这是我的 global.asax:
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional }
);
}
Here is my controller:这是我的控制器:
public class TestController : ApiController
{
[Route("test")]
[HttpPost]
public PaymentResponseModel Response()
{
log.Info("Hello world");
PaymentResponseModel prm = new PaymentResponseModel();
prm.info1 = "tes";
return prm;
}
}
This is the error message that i got:这是我收到的错误消息:
{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:49484/api/test'.",
"MessageDetail": "No route providing a controller name was found to match request URI 'http://localhost:49484/api/test'"
}
This is my url that i tested:这是我测试的网址:
http://localhost:49484/api/test
You have not mentioned action in routing.您没有提到路由中的操作。 Remove
[Route("test")]
.删除
[Route("test")]
。
Try below code.试试下面的代码。
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { action = "Response",
id = System.Web.Http.RouteParameter.Optional }
);
}
If you want to add route path
on action level use MapHttpAttributeRoutes();
如果要在操作级别添加
route path
,请使用MapHttpAttributeRoutes();
You need to have action in your routeTemplate.您需要在您的 routeTemplate 中进行操作。 You can do this:
你可以这样做:
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = "Response",
id = System.Web.Http.RouteParameter.Optional }
);
}
and test it like:并测试它:
http://localhost:49484/api/test
Or you can also do或者你也可以这样做
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional }
);
}
and test it like:并测试它:
http://localhost:49484/api/test/Response
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.