問題
想創建一個從 ASP.NET MVC controller 到 ASP.NET Web API controller 的直接鏈接,或者反向鏈接。
解決方案
可以使用 System.Web.Http.Routing.UrlHelp 的實例來創建一個指向 Controller的鏈接,來暴露ApiController(作為 Url 屬性)。著和在 RequestContext 上一樣,會被附加到 HttpRequestMessage 實例。為了達到這個目的,我們需要調用鏈接方法或路由方法,然后傳入 MVC 路由的名稱和默認路由(Controller 名字,Action名字,以及 Action 相關的參數)。
在 MVC Controller 這邊,System.Web.Mvc.UrlHelp,掛在基礎 MVC 基礎 Controller類,可以通過HttpRouteUrl 生成 Web API 鏈接
工作原理
當使用 ASP.NET Web API 作為現有 MVC 應用程序一部分的時候,有一種很常見的需求,就是在兩種類型的Controller 之間可以互相鏈接。當我們從 Web API 上創建一個到MVC Controller 的鏈接的時候,實際上使用的方法和創建兩個 Web API Controller 之間鏈接的方法完全相同:UrlHelper 中的鏈接或者路由。鏈接和路由生成的鏈接還是有一些區別的,
-
鏈接方法將會生成一個絕對鏈接
-
路由方法生成的是一個相對鏈接。
反過來,我們從 MVC 鏈接到 Web API的時候,HttpRouteUrl 并不是 ASP.NET Web API 程序集的擴展方法,而是 UrlHelper 類的成員,在System.Web.Mvc 中。這個 Helper 使用了一個私有的常量叫做 httproute,每次使用 HttpRouteUrl 的時候,他都會被添加到 RouteValueDictionray 中。
注意 我們將會在 3-12 的時候深入學習和理解引擎生成鏈接到路由背后的故事。
代碼演示
假設一個簡單的關于書籍的 Web 應用程序。如清單 1-10 所示的簡單的 Book 模型,存儲使用的是內存, 配置了API/MVC 路由。這個例子的目的是,在 Web API 和 MVC 控制器之間,完美的使用同一個模型。我們將使用在這個清單中的偽數據來說明 Web API 和 MVC 之間互相鏈接的情況。
清單 1-10. 模型案例,路由和內存存儲
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
public class Book { public int Id { get ; set ; } public string Author { get ; set ; } public string Title { get ; set ; } public string Link { get ; set ; } } public static class Books { public static List<Book> List = new List<Book> { new Book {Id = 1, Author = "John Robb" , Title = "Punk Rock: An Oral History" }, new Book { Id = 2, Author = "Daniel Mohl" , Title = "Building Web, Cloud, and Mobile Solutions with F#" }, new Book { Id = 3, Author = "Steve Clarke" , Title = "100 Things Blue Jays Fans Should Know & Do Before They Die" }, new Book { Id = 4, Author = "Mark Frank" , Title = "Cuban Revelations: Behind the Scenes in Havana " } }; } public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" ); routes.MapRoute( name: "BookPage" , url: "books/details/{id}" , defaults: new {controller = "BooksPage" , action = "Details" } ); } } public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi" , routeTemplate: "api/{controller}/{id}" , defaults: new {id = RouteParameter.Optional} ); } } |
如清單 1-11 所示,這段代碼是為了創建一個從 Web API 到 MVC Controller 的鏈接。BooksPageController 負責處理書籍。為了生成鏈接,我們可以調用 UrlHelper 的鏈接方法,然后傳相關路由的值。
清單 1-11 ASP.NET Web API ApiController 鏈接到 MVC Controller
1
2
3
4
5
6
7
8
|
public class BooksController : ApiController{ public Book GetById( int id) { var book = Books.List.FirstOrDefault(x => x.Id == id); if (book == null ) throw new HttpResponseException(HttpStatusCode.NotFound); book.Link = Url.Link( "BookPage" , new {controller = "BooksPage" , action = "Details" , id}); return book; } |
反方向的鏈接,如清單 1-12 所示,從 MVC Controller 到 ApiController。在這樣的情況下,使用一個 MVC 特定的方法-UrlHelper,他是由 HttpRouteUrl 擴展的方法。
清單 1-12. 從 MVC Controller 鏈接到 ASP.NET Web API
1
2
3
4
5
6
7
8
9
|
public class BooksPageController : Controller{ public ActionResult Details( int id) { var book = Books.List.FirstOrDefault(x => x.Id == id); if (book == null ) return new HttpNotFoundResult(); book.Link = Url.HttpRouteUrl( "DefaultApi" , new {controller = "Books" , id}); return View(book); } } |
文章列表