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