1
我试图通过http://localhost:13489/api/User/TestUser/somestring访问API,但我得到以下错误:.NET API路由错误
<Error>
<Message>
No HTTP resource was found that matches the request URI
'http://localhost:13489/api/User/TestUser/dada'.
</Message>
<MessageDetail>
No action was found on the controller 'User' that matches the name
'TestUser'.
</MessageDetail>
的方法如下
[Authorize]
[EnableCors(origins: "http://i311061.venus.fhict.nl", headers: "*", methods: "*")]
public class UserController : ApiController
{ [Route("api/User/TestUser/{anystring}")]
[HttpGet]
public string TestUser(string json)
{
return json;
}
}
定义路线在WebApiConfig.cs中如下:
config.MapHttpAttributeRoutes()
config.EnableCors();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
我不知道我做错了什么。路线是以错误的方式定义还是存在另一个问题?先谢谢你。
我想你应该重命名{anystring}到{} JSON字符串或JSON字符串anystring ......到底解决了,你的路线占位符应该匹配方法参数的名称。 – hellwd
@hellwd我尝试了你所说的并且错误改成了'Error>这个请求的授权被拒绝。 ' –
MonicaS
是的,所以你最初的问题得到解决:)你得到的新错误是因为你没有通过身份验证,你的控制器是用[Authorize]属性装饰的。或者用你的API获取一些cookie或标记,或者在测试时删除[Authorize]属性:) – hellwd