【问题标题】:Web API Nested Resource in URLURL 中的 Web API 嵌套资源
【发布时间】:2013-05-06 23:58:06
【问题描述】:

我正在创建我的第一个 ASP.NET Web API。我正在尝试遵循标准的 REST URL。我的 API 会返回搜索结果记录。我的网址应该是 -

../api/categories/{categoryId}/subcategories/{subCategoryId}/records?SearchCriteria

我计划在 IIS 上使用 oData 进行搜索和基本/摘要式身份验证。我的问题出在嵌套资源中。在我返回搜索结果之前,我需要检查用户是否可以访问该类别和子类别。 现在我创建了我的 Visual Studio 2012 – MVC4 / Web API 项目。在 App_Start 文件夹中,我认为有 2 个文件与 URL 和资源顺序相关。

1.RouteConfig.cs

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

2.WebApiConfig.cs

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

使用此模型,如果我的 URL 是 ../api/records?SearchCriteria 但它不是我上面提到的 URL 设计,它可以正常工作。我知道我需要做更多的阅读,但到目前为止还没有找到正确的文章。需要您就如何实现我的 URL 以及这两个文件需要进行哪些更改提出建议。或者,我在这里缺少一些其他配置吗?提前致谢。

【问题讨论】:

    标签: asp.net url asp.net-web-api


    【解决方案1】:

    Asp.net Web API 2 提供开箱即用的属性路由。您可以在单个操作方法或全局级别定义Route

    例如:

    [Route("customers/{customerId}/orders/{orderId}")]
    public Order GetOrderByCustomer(int customerId, int orderId) { ... }
    

    您还可以使用[RoutePrefix] 属性为整个控制器设置一个公共前缀:

    [RoutePrefix("api/books")]
    public class BooksController : ApiController
    {
        // GET api/books
        [Route("")]
        public IEnumerable<Book> Get() { ... }
    
        // GET api/books/5
        [Route("{id:int}")]
        public Book Get(int id) { ... }
    }
    

    您可以访问this 链接以获取有关 Web API 2 中属性路由的更多信息。

    【讨论】:

      【解决方案2】:

      假设你有一个名为 categories 的控制器,你的 WebApiConfig.cs 可能有这样的路由来匹配你想要的 url(我个人会离开 /records 部分):

      config.Routes.MapHttpRoute(
          name: "DefaultApi",
          routeTemplate: "api/{controller}/{categoryId}/subcategories/{subCategoryId}",
          defaults: new { controller = "categories", categoryId = somedefaultcategory, 
              subCategoryId = RouteParameter.Optional }
      );
      

      一个方法可能如下所示:

      // search a single subcategory
      public IQueryable<SearchRecord> Get(int categoryId, int subCategoryId = 0, string SearchCriteria = "")
      {        
          // test subCategoryId for non-default value to return records for a single 
          // subcategory; otherwise, return records for all subcategories
          if (subCategoryId != default(int))
          {
          }
      }
      

      但是,如果您还想只返回类别而不是子类别怎么办?在第一个更通用的路线之后,您需要一条额外的路线:

      config.Routes.MapHttpRoute(
          name: "Categories",
          routeTemplate: "api/{controller}/{categoryId}",
          defaults: new { controller = "categories", categoryId = RouteParameter.Optional }
      );
      

      有两种方法,例如:

      // search a single category
      public IQueryable<SearchRecord> Get(int categoryId, string SearchCriteria = "")
      {
      }
      // search all categories
      public IQueryable<SearchRecord> Get(string SearchCriteria = "")
      {
      }
      

      【讨论】:

        猜你喜欢
        • 2020-09-09
        • 2016-09-18
        • 2013-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-17
        • 1970-01-01
        相关资源
        最近更新 更多