C# 没有找到与请求URI匹配的HTTP资源,我需要解决方案
尝试测试web api url时出错 这是我的global.asax:C# 没有找到与请求URI匹配的HTTP资源,我需要解决方案,c#,asp.net-web-api,C#,Asp.net Web Api,尝试测试web api url时出错 这是我的global.asax: void Application_Start(object sender, EventArgs e) { RouteTable.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defa
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional }
);
}
这是我的控制器:
public class TestController : ApiController
{
[Route("test")]
[HttpPost]
public PaymentResponseModel Response()
{
log.Info("Hello world");
PaymentResponseModel prm = new PaymentResponseModel();
prm.info1 = "tes";
return prm;
}
}
这是我收到的错误消息:
{
"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'"
}
这是我测试的url:
http://localhost:49484/api/test
您没有提到路由中的操作。删除
[路由(“测试”)]
试试下面的代码
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 }
);
}
如果要在操作级别添加
路由路径
,请使用MapHttpAttribute路由()代码>您需要在routeTemplate中执行操作。您可以这样做:
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 }
);
}
然后像这样测试它:
http://localhost:49484/api/test
http://localhost:49484/api/test/Response
或者你也可以这样做
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 }
);
}
然后像这样测试它:
http://localhost:49484/api/test
http://localhost:49484/api/test/Response
如何测试端点?和邮递员?还有别的吗?你在发帖子吗?你明白GET vs POST吗?有效吗?@mjwills不,它不起作用,我用POSTANI测试过,问的问题比你回答的多得多。