[水煮 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 ...
随机推荐
- Codeforces Round #341 (Div. 2)B
B. Wet Shark and Bishops time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Tree and Permutation dfs hdu 6446
Problem Description There are N vertices connected by N−1 edges, each edge has its own length.The se ...
- HDU1530 最大团 模板
Maximum Clique Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- JavaScript知识递归实现数组中指定后代元素的查找
描述:要求将下面的数据类型中每个子孙后代id放入一个数组并返回 var arr = [ {"id":28,"text":"公司信息", &q ...
- 本地更新代码同步至github仓库
昨晚在家里写了一个demo放到github上,然后今天晚上来公司准备搞一下,但是git pull下来在本地修改之后push不到github上,然后发现公司电脑上并没有access权限,然后想起来还没配 ...
- python内置函数lambda、filter、map、reduce
lambda匿名函数 1.lambda只是一个表达式,函数体比def简单多. 2.lambda的主体是一个表达式,而不是一个代码块.仅仅能在lambda表达式中封装有限的逻辑进去 3.lambda函数 ...
- CODE FESTIVAL 2017 qual B C - 3 Steps
Score : 500 points Problem Statement Rng has a connected undirected graph with N vertices. Currently ...
- 【洛谷 P4735】 最大异或和 (可持久化Trie)
题目链接 维护整个数列的异或前缀和和\(s\),然后每次就是要求\(s[N]\text{^}x\text{^}s[k],l-1<=k<=r-1\)的最大值 如果没有\(l\)的限制,那么直 ...
- nyoj 15 括号匹配(二) (经典dp)
题目链接 描述 给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些 ...
- Bitmap算法应用实例(转)
关于数据库查询工作,例如有如下数据库表 要想统计所有90后的程序员该怎么做呢?用一条求交集的SQL语句即可: Select count(distinct Name) as 用户数 from table ...