【发布时间】:2018-01-30 07:55:04
【问题描述】:
我不明白为什么我的属性路由不起作用。如果我转到“http://localhost:56125/api/Contact/1682”,那么我会得到显示“value1”和“value2”的测试控制器,但如果我转到http://localhost:56125/api/contacts/1682,那么我会得到No HTTP resource was found that matches the request URI 'http://localhost:56125/api/contacts/1682',我不明白为什么?
在 ContactController.cs 中:
public class ContactController : ApiController
{
private readonly NGSystemRepository _repo = new NGSystemRepository();
// GET: api/Contact
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
[Route("api/contacts/{contactId}")]
[HttpGet]
public ContactInformation GetContactInformation(int contactNumber)
{
return _repo.GetContact(contactNumber);
}
}
在 WebApiConfig 中:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
【问题讨论】:
标签: asp.net-web-api routing asp.net-web-api2 attributerouting