Web API 2路由属性在一个控制器中工作,但不在另一个控

时间:2013-11-11 19:26:10

标签: c# asp.net asp.net-mvc-4 asp.net-web-api asp.net-web-api-routing

使用.NET 4.5.1,Web API 2,Visual Studio 2013:

我有一个Web API,它有以下路由......

  • / API /提供商/特色
  • / API /提供商/特色/ 123
  • / API /提供商/特色/药

这些按预期工作......第一个获得所有专业的列表,第二个获得专业ID 123,第三个获得名称中“药”的所有专业。

我也有这些路线......

  • / API /位置/特色
  • / API /位置/特色/ 123
  • / API /位置/特色/邻

只有最后两个工作...第一个返回此错误:

No HTTP resource was found that matches the request URI [...]
No route providing a controller name was found to match request URI 'http://mysite/api/locations/specialties'

这怎么可能?它会击中该控制器中的其他路径,而不是基础路径。

(我还有两个其他控制器,路由 / api / providers / api / locations ,它们可以正常工作。 )

以下是 ProviderSpecialtyController.cs 代码:

[RoutePrefix("api/providers/specialties")]
public class ProviderSpecialtyController : ApiController
{
    private ProviderEntities db = new ProviderEntities();

    /// <summary>
    /// Get ALL specialties, sorted by name.
    /// </summary>
    [Route("")]
    public IQueryable<ProviderSpecialty> Get()
    {
        return db.ProviderSpecialties.OrderBy(s => s.Name);
    }

    /// <summary>
    /// Get a specific specialty.
    /// </summary>
    /// <param name="id">The ID of a particular specialty.</param>
    [Route("{id:int}")]
    public ProviderSpecialty Get(int id)
    {
        return db.ProviderSpecialties.Where(s => s.Id == id).FirstOrDefault();
    }

    /// <summary>
    /// Get all specialties that contain a keyword.
    /// </summary>
    /// <param name="keyword">The keyword to search for in a specialty name.</param>
    [Route("{keyword:alpha}")]
    public IQueryable<ProviderSpecialty> Get(string keyword)
    {
        return db.ProviderSpecialties.Where(s => s.Name.Contains(keyword)).OrderBy(s => s.Name);
    }
}

这是 LocationSpecialtyController.cs 代码:

[RoutePrefix("api/locations/specialties")]
public class LocationSpecialtyController : ApiController
{
    private ProviderEntities db = new ProviderEntities();

    /// <summary>
    /// Get ALL specialties, sorted by name.
    /// </summary>
    [Route("")]
    public IQueryable<LocationSpecialty> Get()
    {
        return db.LocationSpecialties.OrderBy(s => s.Name);
    }

    /// <summary>
    /// Get a specific specialty.
    /// </summary>
    /// <param name="id">The ID of a particular specialty.</param>
    [Route("{id:int}")]
    public LocationSpecialty Get(int id)
    {
        return db.LocationSpecialties.Where(s => s.Id == id).FirstOrDefault();
    }

    /// <summary>
    /// Get all specialties that contain a keyword.
    /// </summary>
    /// <param name="keyword">The keyword to search for in a specialty name.</param>
    [Route("{keyword:alpha}")]
    public IQueryable<LocationSpecialty> Get(string keyword)
    {
        return db.LocationSpecialties.Where(s => s.Name.Contains(keyword)).OrderBy(s => s.Name);
    }
}

如您所见,除路线前缀外,它们几乎相同。为什么提供程序控制器按预期工作但位置控制器不工作?

我启用了跟踪功能,在尝试点击 / api / locations / specialties 时会出现以下情况:

System.Web.Http.Request: GET http://localhost:49565/api/locations/specialties/: Category=System.Web.Http.Request, Level=Info Begin   http://localhost:49565/api/locations/specialties/
System.Web.Http.Controllers: GET http://localhost:49565/api/locations/specialties/: Category=System.Web.Http.Controllers, Level=Info Begin DefaultHttpControllerSelector SelectController Route='MS_SubRoutes:System.Web.Http.Routing.IHttpRouteData[]'
[...]
System.Web.Http.Controllers: GET http://localhost:49565/api/locations/specialties/: Category=System.Web.Http.Controllers, Level=Error End DefaultHttpControllerSelector SelectController Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details.

1 个答案:

答案 0 :(得分:1)

这比看起来更简单,但是由于调试不当(filed and verified as a bug在Codeplex中为Kiran Challa而确定原因变得更加困难。这应该在Web API 2.1中修复。

我有一个这条路线的控制器:

/api/locations/keyword

哪个会对keyword进行关键字搜索。

我有另一个控制器使用这些路线:

/api/locations/specialties
/api/locations/specialties/123
/api/locations/specialties/keyword

API引擎很困惑,因为我有两个基本相同路径的控制器。我删除了一个,问题得到解决。

根据Codeplex问题跟踪器,问题已经过验证,已关闭,并在Web API 2.1中添加了新的错误消息。