您好,登錄后才能下訂單哦!
問題
想創(chuàng)建一個從 ASP.NET MVC controller 到 ASP.NET Web API controller 的直接鏈接,或者反向鏈接。
解決方案
可以使用 System.Web.Http.Routing.UrlHelp 的實(shí)例來創(chuàng)建一個指向 Controller的鏈接,來暴露 ApiController(作為 Url 屬性)。著和在 RequestContext 上一樣,會被附加到 HttpRequestMessage 實(shí)例。為了達(dá)到這個目的,我們需要調(diào)用鏈接方法或路由方法,然后傳入 MVC 路由的名稱和默認(rèn)路由(Controller 名字,Action名字,以及 Action 相關(guān)的參數(shù))。
在 MVC Controller 這邊,System.Web.Mvc.UrlHelp,掛在基礎(chǔ) MVC 基礎(chǔ) Controller類,可以通過 HttpRouteUrl 生成 Web API 鏈接
工作原理
當(dāng)使用 ASP.NET Web API 作為現(xiàn)有 MVC 應(yīng)用程序一部分的時候,有一種很常見的需求,就是在兩種類型的 Controller 之間可以互相鏈接。當(dāng)我們從 Web API 上創(chuàng)建一個到MVC Controller 的鏈接的時候,實(shí)際上使用的方法和創(chuàng)建兩個 Web API Controller 之間鏈接的方法完全相同:UrlHelper 中的鏈接或者路由。鏈接和路由生成的鏈接還是有一些區(qū)別的,
鏈接方法將會生成一個絕對鏈接
路由方法生成的是一個相對鏈接。
反過來,我們從 MVC 鏈接到 Web API的時候,HttpRouteUrl 并不是 ASP.NET Web API 程序集的擴(kuò)展方法,而是 UrlHelper 類的成員,在System.Web.Mvc 中。這個 Helper 使用了一個私有的常量叫做 httproute,每次使用 HttpRouteUrl 的時候,他都會被添加到 RouteValueDictionray 中。
注意 我們將會在 3-12 的時候深入學(xué)習(xí)和理解引擎生成鏈接到路由背后的故事。
代碼演示
假設(shè)一個簡單的關(guān)于書籍的 Web 應(yīng)用程序。如清單 1-10 所示的簡單的 Book 模型,存儲使用的是內(nèi)存, 配置了API/MVC 路由。這個例子的目的是,在 Web API 和 MVC 控制器之間,完美的使用同一個模型。我們將使用在這個清單中的偽數(shù)據(jù)來說明 Web API 和 MVC 之間互相鏈接的情況。
清單 1-10. 模型案例,路由和內(nèi)存存儲
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 所示,這段代碼是為了創(chuàng)建一個從 Web API 到 MVC Controller 的鏈接。BooksPageController 負(fù)責(zé)處理書籍。為了生成鏈接,我們可以調(diào)用 UrlHelper 的鏈接方法,然后傳相關(guān)路由的值。
清單 1-11 ASP.NET Web API ApiController 鏈接到 MVC Controller
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 擴(kuò)展的方法。
清單 1-12. 從 MVC Controller 鏈接到 ASP.NET Web API
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); } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。