asp.net mvc4 之Webapi之應用Client Access Server端

來源:互聯網
上載者:User

標籤:路由規則   建立   visual   控制   success   end   替換   引用   用戶端   

一、說明

用戶端項目類型設計為:winform(winform表單項目類型)

伺服器端項目類型設計為:asp.net mvc4  webapi

在這裡分為項目運行和調試兩種情況討論:

運行:

       這種情況指的是伺服器端項目已經開發完成,可以把其部署到iis中(http://localhost:8748)括弧裡是伺服器端部署到iis上的訪問地址,Client Access Server時要使用

 

調試:

調試的時候可以把用戶端和伺服器端建立在同一個解決方案下。調試步驟:先要運行伺服器項目,然後在托盤裡查看伺服器的訪問地址

把伺服器訪問地址賦值給我用戶端,運行Client Access Server端即可。

 

二、建立伺服器端項目

首先開啟vs2012開發環境軟體,

1、建立空白的解決方案

操作步驟:檔案—》建立—》項目—》其他項目類型—》visual studio 解決方案

解決方案命名為:MyTest

 

2、建立伺服器端

按右鍵上一步建立的解決方案—》添加—》建立項目—》web-->asp.net mvc4 web應用程式(命名為:MyServer)--》web api(mvc項目模板)

3、添加webapi控制器

按右鍵MyServer項目中的Controllers檔案夾—》添加—》控制器(命名為User)--》空API控制器

在控制器中添加方法

  [HttpGet]

        public string GetUserInfo(string userName, string passWord)

        {

            if (userName == "admin" && passWord == "123456")

            {

                return "success";

            }

            else

            {

                return "failed";

            }

 

        }

 

另外在檔案WebApiConfig中添加自訂的路由規則

       config.Routes.MapHttpRoute(

            name: "MyApi",

            routeTemplate: "api/{controller}/{action}/{key}",

            defaults: new { key = RouteParameter.Optional }

        );

 

三、建立用戶端(client)

按右鍵解決方案“MyTest”—》添加—》建立項目—》Windows—》Windows表單應用程式 (並命名為:MyClient)

在預設的表單Form1上設計介面如下:

表單form1的後台代碼如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Net.Http;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace MyClient

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void btnSubmit_Click(object sender, EventArgs e)

        {

            string userName = txName.Text.Trim();

            string passWord = txPwd.Text.Trim();

            string url = @"http://localhost:47673/api/File/GetUserInfo?userName=" + userName + "&passWord="+passWord;

            HttpClient client = new HttpClient();

            HttpResponseMessage response = client.GetAsync(url).Result;      

            string str=  response.Content.ReadAsStringAsync().Result;

            MessageBox.Show(str);

        }

    }

}

 注意在用戶端引用程式集 System.Net.Http;引用方法:按右鍵MyClient中的引用檔案夾—》添加引用—》程式集

同時在form1的後台代碼中也要添加using System.Net.Http;引用

 四、調試

1、在vs2012中,選中伺服器端項目,編譯通過後,執行“開始執行(不調試)”

2、查看伺服器的訪問地址,方法如

 在托盤中按右鍵IIS Express

 

把用戶端後台代碼中的訪問地址URL替換為:

string url = @"http://localhost:8748/api/User/GetUserInfo?userName=" + userName + "&passWord="+passWord;

 3、啟動用戶端項目

在解決方案中,選中用戶端項目(設為啟動項目),MyClient啟動調試即可

asp.net mvc4 之Webapi之應用Client Access Server端

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.