返璞歸真 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[0].Headers; // 上傳文件相關的頭信息
            result += "<br />";
            result += "fileData localFileName: " + bodyparts.FileData[0].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(10 * 1000);

            return "長時間的任務執行完畢";
        }
    }
}



OK
[源碼下載]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM