【发布时间】:2016-09-30 20:18:36
【问题描述】:
我想在我的 Web Api 中添加一条新路由,它会读取各种 id,然后过滤一堆书。
所以最终的 url 应该是 http://localhost/api/books/1/1/1/1
现在我添加了一条到我的RouteConfig 的路由,如下所示:-
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "BookFilter",
url: "api/books/{author}/{title}/{genre}/{isbn}",
defaults: new { controller = "Books", action = "BookFilter" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
我还在我的BooksController 中添加了以下内容:-
[HttpGet]
public IQueryable<BookDTO> BookFilter(int authorId, int titleId, int genreId, int isbn)
{
//filter books here
return db.Books.ProjectTo<BookDTO>();
}
但是,当我尝试访问该页面时,我得到了 404。
我需要做什么才能访问我的页面?
感谢您的帮助和时间
【问题讨论】:
-
知道
attribute routing吗? -
是的,我有一个想法,只是我有一段时间没有这样做了
标签: asp.net-mvc asp.net-web-api2 url-routing asp.net-mvc-routing asp.net-web-api-routing