[水煮 ASP.NET Web API2 方法论](1-2)在 WebForm 应用程序中添加 ASP.NET Web API
问题
怎么样将 Asp.Net Web Api 加入到 Asp.Net Web From 应用程序中
解决方案
在 Visual Studio 2013 中,创建新的 Web From,可以直接在"新建 ASP.NET 项目" 创建项目向导中,勾选ASP.NET Web API ,将其加入进来。如图 1-2 所示。

图 1-2. 在Asp.NET 项目向导,同时选中 Web Form 和 Web API
因为可以通过 NuGet 添加 ASP.NET Web API ,所以使用“Install-Package Microsoft.AspNet.WebApi”就可以轻易将其添加到现有的 Web Form 解决方案中。
在 Visual Studio 2012 中使用也很简单,只要创建一个 WebForm 项目,然后通过NuGet 来安装 Web API 就可以。
工作原理
和在 MVC 中使用 ASP.NET Web API 一样,在 Web Form 项目中ASP.NET Web API 使用 的结果就是,Web API和 Web Form 应用程序运行在同一个 ASP.NET 进程中。
在 ASP.NET 项目中安装 Microsoft.AspNet.WebApi NuGet 包时,会在 App_Start文件夹中添加 WebApiConfig 的 静态类。这个文件是用来配置 ASP.NET Web API 和定义 ASP.NET Web API 路由。
另外,在 Global.asax 中的 Application_Start 可以找到被添加的代码,就像下面的代码片段,调用 Web API配置。
|
1
|
GlobalConfiguration.Configure(WebApiConfig.Register); |
Web API 运行在 Web Form 应用程序中与 运行在 MVC 应用程序中没什么不同。每个请求仍将被相关的IHttpHandler 处理。可能是用于处理 Web API 的 HttpControllerHandler 或者是用于处理 Web Form 的处理器。Web Form 相关的 ASPX 扩展名会交给 PageHandlerFactory,依次调用相关的 IHttpHandler 来处理 HTTP请求。System.Web.UI.Page 类是 Web Form 应用程序的默认组成部分,也是一个 IHttpHandler,其实他才是请求处理器的真正执行者。
代码演示
清单 1-5 展示了一个简单的模型类,这个模型是ApiController 和 Web Form 页展示数据的共享类。
清单 1-5. 简单模型,Web Form 页,和 Web API 控制器
|
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
|
public class Book{ public int Id { get; set; } public string Author { get; set; } public string Title { get; set; }}public partial class _Default : Page{ protected void Page_Load(object sender, EventArgs e) { int id; if (Int32.TryParse((string)Page.RouteData.Values["id"], out id)) { var book = Books.List.FirstOrDefault(x => x.Id == id); if (book == null) { Response.StatusCode = 404; return; } ltlAuthor.Text = book.Author; ltlTitle.Text = book.Title; hplLink.NavigateUrl = "/api/books/" + book.Id; } Response.StatusCode = 404; }}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); return book; }} |
这是一个约定,在解决方案的 Cotrollers 文件夹中放 ApiController,但是,这并不意味着这是强制要求;在当前应用程序中,只要被声明为 public 的类,类名以Controller 为后缀的 IHttpController 实现类,都会被运行时发现,也会被当成一个可以处理的 HTTP 请求。
就像 Web API 和 MVC 一块儿运行一样,当使用 Web Form 路由,我们也必须留心那些要被 Web API 处理的路由和那些要导向 ASPX 页面之间引起的冲突。列表 1-6 展示了 Web Form 和 Web API的简单路由设置。ASP.NET Web API 路由是在 WebApiConfig 的静态类中设置的,然而,Web Form 路由是在RouteConfig 静态类中设置的。
列表 1-6. Web API 路由和 Web Form 路由
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public static class RouteConfig{ public static void RegisterRoutes(RouteCollection routes) { var settings = new FriendlyUrlSettings(); settings.AutoRedirectMode = RedirectMode.Permanent; routes.EnableFriendlyUrls(settings); routes.MapPageRoute( "book-route", "book/{id}", "~/default.aspx"); }}public static class WebApiConfig{ public static void Register(HttpConfiguration config) { // Web API configuration and services // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }} |
[水煮 ASP.NET Web API2 方法论](1-2)在 WebForm 应用程序中添加 ASP.NET Web API的更多相关文章
- [水煮 ASP.NET Web API2 方法论](1-1)在MVC 应用程序中添加 ASP.NET Web API
问题 怎么样将 Asp.Net Web Api 加入到现有的 Asp.Net MVC 项目中 解决方案 在 Visual Studio 2012 中就已经把 Asp.Net Web Api 自动地整合 ...
- [水煮 ASP.NET Web API2 方法论](3-9)空气路由的设置
阅读导航 问题 解决方案 工作原理 代码演示 在此解释一下,空气路由,是本人臆想出来,觉着更能表达 IgnoreRoute 的意图,如果看着辣眼睛^^,请见谅. 问题 我们在之定义过集中式路由,集中式 ...
- visual studio 2010 winform程序不能添加对system.web的引用
visual studio 2010 winform程序不能添加对system.web的引用[转载] 需要引用到System.Web.发现没有“System.Web”.在通过“浏览”方式,找到该DLL ...
- [水煮 ASP.NET Web API2 方法论](1-5)ASP.NET Web API Scaffolding(模板)
问题 我们想快速启动一个 ASP.NET Web API 解决方案. 解决方案 APS.NET 模板一开始就支持 ASP.NET Web API.使用模板往我们的项目中添加 Controller,在我 ...
- [水煮 ASP.NET Web API2 方法论](3-2)直接式路由/属性路由
问题 怎么样可以使用更贴近资源(Controller,Action)的方式定义路由. 解决方案 可以使用属性路由直接在资源级别声明路由.只要简单的在 Action 上使用属性路由 RouteAttri ...
- [水煮 ASP.NET Web API2 方法论](3-1)集中式路由
问题 怎样集中的定义路由 解决方案 通过调用 HttpRouteCollectionExtension 类中的 MapHttpRoute 扩展方法在 HttpRouteCollection 中定义路由 ...
- [水煮 ASP.NET Web API2 方法论](1-3)如何接收 HTML 表单请求
问题 我们想创建一个能够处理 HTML表单的 ASP.NET Web API 应用程序(使用 application/x-www-form-urlencoded 方式提交数据). 解决方案 我们可以创 ...
- [水煮 ASP.NET Web API2 方法论](1-8)添加 Session 状态
问题 ASP.NET Web API 构建 Web 应用程序时,要求使用 Session 在服务器存储一些用户特定的信息 解决方案 ASP.NET Web API 不支持 Session,因为 API ...
- [水煮 ASP.NET Web API2 方法论](12-1)创建 OData
问题 怎样用在 Web API 中创建 OData 服务. 解决方案 对于我们来说,在 Web API 中使用 OData最简单的方式就是使用 ASP.NET 模板来创建Odata Controlle ...
随机推荐
- HDU4305:Lightning(生成树计数+判断点是否在线段上)
Lightning Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- POJ 2763 Housewife Wind 纯粹LCA写法(简单无脑)
Description After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordina ...
- 与http协作的web服务器、http首部(第五章、第六章)
第五章 与http协作的web服务器 1.用单台虚拟主机实现多个域名 通过域名访问主机,经过DNS解析成ip地址,反向代理,可以代理多台服务器,正向代理则相反,代理客户端 2.通信数据转化程序:代理. ...
- LightOJ 1135 - Count the Multiples of 3 线段树
http://www.lightoj.com/volume_showproblem.php?problem=1135 题意:给定两个操作,一个对区间所有元素加1,一个询问区间能被3整除的数有多少个. ...
- MyBatis框架的使用及源码分析(八) MapperMethod
从 <MyBatis框架中Mapper映射配置的使用及原理解析(七) MapperProxy,MapperProxyFactory> 文中,我们知道Mapper,通过MapperProxy ...
- 【BZOJ1038】【ZJOI2008】瞭望塔 [模拟退火]
瞭望塔 Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Description 致力于建设全国示范和谐小村庄的H村村 ...
- MySQL增删改查之查询
(7)范围查询select * from car where price>40 and price<60 --查询价格在40-60之间的select * from car where ...
- RelativeLayout相对布局中属性值
android:layout_above="@id/xxx" --将控件置于给定ID控件之上 android:layout_below="@id/xxx" ...
- TensorFlow中get_variable共享变量调用
import tensorflow as tf with tf.variable_scope('v_scope',reuse=True) as scope1: Weights1 = tf.get_va ...
- Java面试基础知识1
1.动态绑定是指在执行期间判断所引用对象的实际类型,根据其实际的类型调用其相应的方法. 2.在将超类转换为子类之前,应该使用instanceof进行检查. 3.包含一个或者多个抽象方法的类本身必须被声 ...