MVC Controller 链接到 API Controller 以及反向链接
MVC Controller 链接到 API Controller 以及反向链接
问题
想创建一个从 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); }} |
MVC Controller 链接到 API Controller 以及反向链接的更多相关文章
- [水煮 ASP.NET Web API2 方法论](1-4)从 MVC Controller 链接到 API Controller 以及反向链接
问题 想创建一个从 ASP.NET MVC controller 到 ASP.NET Web API controller 的直接链接,或者反向链接. 解决方案 可以使用 System.Web.Htt ...
- MVC模式(Model View Controller)下实现数据库的连接,对数据的删,查操作
MVC模式(Model View Controller): Model:DAO模型 View:JSP 在页面上填写java代码实现显示 Controller:Servlet 重定向和请求的转发: 若 ...
- ASP.NET Core开发-MVC 使用dotnet 命令创建Controller和View
使用dotnet 命令在ASP.NET Core MVC 中创建Controller和View,之前讲解过使用yo 来创建Controller和View. 下面来了解dotnet 命令来创建Contr ...
- [译] ASP.NET MVC 6 attribute routing – the [controller] and [action] tokens
原文:http://www.strathweb.com/2015/01/asp-net-mvc-6-attribute-routing-controller-action-tokens/ 当在Web ...
- Core开发-MVC 使用dotnet 命令创建Controller和View
NET Core开发-MVC 使用dotnet 命令创建Controller和View 使用dotnet 命令在ASP.NET Core MVC 中创建Controller和View,之前讲解过使 ...
- 测试 ASP.NET Core API Controller
本文需要您了解ASP.NET Core MVC/Web API, xUnit以及Moq相关知识. 这里有xUnit和Moq的介绍: https://www.cnblogs.com/cgzl/p/917 ...
- [ASP.NET MVC 小牛之路]09 - Controller 和 Action (1)
我们知道,在 MVC 中每个请求都会提交到 Controller 进行处理.Controller 是和请求密切相关的,它包含了对请求的逻辑处理,能对 Model 进行操作并选择 View 呈现给用户, ...
- [ASP.NET MVC 小牛之路]10 - Controller 和 Action (2)
继上一篇文章之后,本文将介绍 Controller 和 Action 的一些较高级特性,包括 Controller Factory.Action Invoker 和异步 Controller 等内容. ...
- 创建ASP.NET Core MVC应用程序(1)-添加Controller和View
创建ASP.NET Core MVC应用程序(1)-添加Controller和View 参考文档:Getting started with ASP.NET Core MVC and Visual St ...
随机推荐
- Question2Answer初体验
Question2Answer初体验 高质量的问答社区十分有价值,很多无法解决的问题能通过问答社区找到解决办法,而对于站长来说,垂直的问答社区也很有潜力.最近盯上问答这一块,发现和我的一些思路很符 ...
- C++ builder 操作Excel方法(据网上资料整理)
c++ builder 操作Excel方法,下面是从网上找到的一些不错的方法,学习一下: 用OLE操作Excel(目前最全的资料)(04.2.19更新) 本文档部分资料来自互联网,大部分是ccrun( ...
- java的WebService实践(cxf)
Java发布WebService,结合Spring,通过cxf的方式 难点:1.引用什么jar包: 1.创建接口 源码如下: package com.nankang; import javax.jws ...
- Android IOS WebRTC 音视频开发总结(十七)-- 调试技巧
本文章主要介绍WEBRTC在各平台下调试或日志查看方式,以方便问题排查,包括BS,PC,Android,IOS(本系列文章转载请说明出处,博客园RTC.Blacker). 1,浏览器开发: 这种开发方 ...
- CentOS学习笔记--SCSI 设备热插拔
CentOS学习笔记--SCSI 设备热插拔 处于运行中的服务器,因业务要求也许不允许重启机器,而新添加的SCSI设备(主要是硬盘)如何实现热插拔呢? 首先需要查看一下设备: #cat /proc/s ...
- dwz简单配置与操作
1.首先将dwz的文件放到你的项目中(http://yunpan.cn/QbTH4kN6UXX9B) 2.在页面中将前台数据复制到页面中,将js,css等路径配置好 3.这个地方一定要配置好,xml文 ...
- CentOS 6.x版本升级Mysql
首先确定一下自己的DNS vi /etc/resolv.conf 我一般为114.114.114.114 #-----------------------------CentOS 6.x版本升 ...
- "/Date(1405056837780)/" 时间转换
//往往json传过来的时间都是"/Date(1405056837780)/" //转换需要的方法 String.prototype.ToString = function (fo ...
- 史上最全Vim快捷键键位图(入门到进阶)
经典版 下面这个键位图应该是大家最常看见的经典版了. 对应的简体中文版 其实经典版是一系列的入门教程键位图的组合结果,下面是不同编辑模式下的键位图. 入门版 基本操作的入门版. 进阶版 增强版 下图是 ...
- source insight用于C语言编程的工具脚本
简单来说,source insight提供的功能功能还不够傻瓜,用起来还不够方便,所以写了此脚本,提高开发效率. 部分source insight提供的功能也包含了进来,主要是因为我不喜欢使用太多的快 ...