返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, .net 4.5 带来的更方便的异步操作

2023-05-13,,

原文:返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, .net 4.5 带来的更方便的异步操作

[索引页]
[源码下载]

返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, .net 4.5 带来的更方便的异步操作

作者:webabcd

介绍
asp.net mvc 之 asp.net mvc 4.0 新特性之 Web API

自宿主 web api
宿主到 iis,通过 WebForm 提供 web api 服务
通过 Web API 上传文件
.net 4.5 带来的更方便的异步操作

示例
1、自宿主 Web API 的 demo
WebApiSelfHost/Program.cs

/*
* 自宿主 web api 的 demo
*
* 测试地址:http://localhost:123/api/hello
*/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using System.Web.Http.SelfHost; namespace WebApiSelfHost
{
// web api
public class HelloController : ApiController
{
public string Get()
{
return "hello: webabcd";
}
} class Program
{
static readonly Uri _baseAddress = new Uri("http://localhost:123/"); static void Main(string[] args)
{
HttpSelfHostServer server = null;
try
{
// 配置一个自宿主 http 服务
HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(_baseAddress); // 配置 http 服务的路由
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
); // 创建 http 服务
server = new HttpSelfHostServer(config); // 开始监听
server.OpenAsync().Wait();
// 停止监听
// server.CloseAsync().Wait(); Console.WriteLine("Listening on " + _baseAddress);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
}
}
}
}

2、演示如何在 WebForm 中提供 web api 服务
Global.asax.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Http; namespace WebApiWebFormHost
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
// 配置路由
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}

HelloController.cs

/*
* 宿主到 iis,通过 WebForm 提供 web api 服务
*
* 测试地址:http://localhost:4723/api/hello
*/ using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http; namespace WebApiWebFormHost
{
public class HelloController : ApiController
{
public IEnumerable<string> Get()
{
string[] names = { "webabcd", "webabcd2", "webabcd3" };
return names;
}
}
}

3、演示如何通过 web api 上传文件
WebApiWebFormHost/UploadFileController.cs

/*
* 通过 web api 上传文件
*/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http; namespace MVC40.Controllers
{
public class UploadFileController : ApiController
{
public async Task<string> Post()
{
// 检查是否是 multipart/form-data
if (!Request.Content.IsMimeMultipartContent("form-data"))
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); // 设置上传目录
var provider = new MultipartFormDataStreamProvider(@"c:\\temp"); // 接收数据,并保存文件
var bodyparts = await Request.Content.ReadAsMultipartAsync(provider); string result = "";
// 获取表单数据
result += "formData txtName: " + bodyparts.FormData["txtName"];
result += "<br />"; // 获取文件数据
result += "fileData headers: " + bodyparts.FileData[].Headers; // 上传文件相关的头信息
result += "<br />";
result += "fileData localFileName: " + bodyparts.FileData[].LocalFileName; // 文件在服务端的保存地址,需要的话自行 rename 或 move return result;
}
}
}

WebApiWebFormHost/UploadDemo.cshtml

@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<title>调用 web api 上传文件的 demo</title>
</head>
<body>
@using (Html.BeginForm("UploadFile", "api", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="text" id="txtName" name="txtName" value="webabcd" />
<div>please select a file</div>
<input name="data" type="file" multiple />
<input type="submit" />
}
</body>
</html>

4、.net 4.5 带来的更方便的异步操作
AsyncController.cs

/*
* 演示如何利用 .net 4.5 的新特性实现异步操作
*
* 什么场景下需要异步操作?
* 在因为磁盘io或网络io而导致的任务执行时间长的时候应该使用异步操作,如果任务执行时间长是因为cpu的消耗则应使用同步操作(此时异步操作不会改善任何问题)
* 原理是什么?
* 在 Web 服务器上,.NET Framework 维护一个用于服务 ASP.NET 请求的线程池(以下把 .NET Framework 维护的用于服务 ASP.NET 请求的线程池称作为“特定线程池”)
* 同步操作时,如果特定线程池利用满了,则不会再提供服务
* 异步操作时:
* 1、一个请求过来,特定线程池出一个线程处理此请求
* 2、启动一个非特定线程池中的另一个线程处理异步操作,此时处理此请求的线程就会空出来,不会被阻塞,它可以继续处理其它请求
* 3、异步操作执行完毕后,从特定线程池中随便找一个空闲线程返回请求结果
*/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http; namespace MVC40.Controllers
{
public class AsyncController : ApiController
{
public async Task<string> Get()
{
return await GetData();
} [NonAction]
public async Task<string> GetData()
{
await Task.Delay( * ); return "长时间的任务执行完毕";
}
}
}

OK
[源码下载]

返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, .net 4.5 带来的更方便的异步操作的相关教程结束。

《返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, .net 4.5 带来的更方便的异步操作.doc》

下载本文的Word格式文档,以方便收藏与打印。