【发布时间】:2014-08-27 12:37:33
【问题描述】:
我正在使用 vs2012 并使用“ASP.net Web Api 2”模板开始了一个新项目。完成编码并开始测试系统。我观察到路由的奇怪行为。如:
这是我的路线:
config.Routes.MapHttpRoute(
name: "personRoute",
routeTemplate: "api/px/{action}/{personid:Guid}",
defaults: new { controller = "px", personid = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
这是我的控制器:
public class PxController : ApiController {
private Configuration.DatabaseContext dbctx = new Configuration.DatabaseContext();
[HttpGet]
public IEnumerable<Models.Person> GetAll() {
return dbctx.Persons.ToArray();
}
[ActionName( "op" )]
[HttpGet]
public Models.Person ById( Guid personid ) {
var data = dbctx.Persons.FirstOrDefault( e => e.PersonId == personid );
if (data == null) {
throw new HttpResponseException( HttpStatusCode.NotFound );
}
return data;
}
[ActionName( "op" )]
[HttpPost]
public void Insert( [FromBody] Models.Person newPerson ) {
dbctx.Persons.Add( newPerson );
dbctx.SaveChanges();
}
[ActionName( "op" )]
[HttpPut]
public void Update( [FromBody]Models.Person eperson ) {
var data = dbctx.Persons.FirstOrDefault( e => e.PersonId == eperson.PersonId );
if (data == null) {
throw new HttpResponseException( HttpStatusCode.NotFound );
}
dbctx.Entry( eperson ).State = System.Data.Entity.EntityState.Modified;
dbctx.SaveChanges();
}
[ActionName( "op" )]
[HttpDelete]
public void DeleteById( Guid personid ) {
var data = dbctx.Persons.FirstOrDefault( e => e.PersonId == personid );
if (data == null) {
throw new HttpResponseException( HttpStatusCode.NotFound );
}
dbctx.Entry( data ).State = System.Data.Entity.EntityState.Deleted;
dbctx.SaveChanges();
}
#region |:.Extended Queries.:|
[HttpGet]
public IEnumerable<RemoteProperty> Properties( Guid personid ) {
var data = from n in dbctx.RemoteProperties
where n.PersonId == personid && n.ParentId == null
orderby n.PropertyType.Name
select n;
if (data == null)
throw new HttpResponseException( HttpStatusCode.NotFound );
foreach (var rp in data) {
rp.Details = dbctx.RemoteProperties.SqlQuery( "SELECT * FROM properties WHERE parentid = @parid" ).ToArray();
}
return data.ToArray();
}
[HttpGet]
public IEnumerable<Relation> Relations( Guid personid ) {
var data = from n in dbctx.Relations
where n.PersonId == personid
select n;
if (data == null)
throw new HttpResponseException( HttpStatusCode.NotFound );
return data.ToArray();
}
#endregion
[NonAction]
protected override void Dispose( bool disposing ) {
if (dbctx != null && dbctx is IDisposable) {
dbctx.Dispose();
}
base.Dispose( disposing );
}
}
这些是查询的 Fiddler 输出:
http://localhost:49318/api/px/GetAll
/* Works as expected */
http://localhost:49318/api/px/op/dfc737ca-312c-e411-ae3c-78843ccba6ef
/* The requested resource does not support http method 'GET'. */
http://localhost:49318/api/px/op?personid=dfc737ca-312c-e411-ae3c-78843ccba6ef
/* Returns the data with personid, but why the above query fails? */
http://localhost:49318/api/px/properties?personid=dfc737ca-312c-e411-ae3c-78843ccba6ef
/* Returns the data with personid */
http://localhost:49318/api/px/properties/dfc737ca-312c-e411-ae3c-78843ccba6ef
/* No action was found on the controller 'Px' that matches the request */
我的问题是这种行为的原因是什么?我也不能使用类似的路线(因为它们不起作用):
"api/{controller}/{personid}/{action}"
【问题讨论】:
标签: c# asp.net-mvc-routing asp.net-web-api2