zoukankan      html  css  js  c++  java
  • asp.net mvc4 之Webapi之应用客户端访问服务器端

    一、说明

    客户端项目类型设计为:winform(winform窗体项目类型)

    服务器端项目类型设计为:asp.net mvc4  webapi

    在这里分为项目运行和调试两种情况讨论:

    运行:

           这种情况指的是服务器端项目已经开发完成,可以把其部署到iis中(http://localhost:8748)括号里是服务器端部署到iis上的访问地址,客户端访问服务器时要使用

     

    调试:

    调试的时候可以把客户端和服务器端创建在同一个解决方案下。调试步骤:先要运行服务器项目,然后在托盘里查看服务器的访问地址

    把服务器访问地址赋值给我客户端,运行客户端访问服务器端即可。

    二、创建服务器端项目

    首先打开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启动调试即可

  • 相关阅读:
    C# in Depth Third Edition 学习笔记-- Lambda表达式和表达式树
    几个比较实用的.Net 反编译工具
    使用Microsoft.Practices.EnterpriseLibrary.Data调用存数过程Output参数注意事项
    C# in Depth Third Edition 学习笔记-- C#2的一些特性
    C# in Depth Third Edition 学习笔记-- 可空类型
    C# in Depth Third Edition 学习笔记-- C#2.0: 解决C#1.0的问题 1 泛型
    C# in Depth Third Edition 学习笔记-- 值类型和引用
    .Net 程序员应该知道的工具和网站
    HTML 转 PDF
    C#、ASP.NET获取当前应用程序的绝对路径,获取程序工作路径 (转帖)
  • 原文地址:https://www.cnblogs.com/net064/p/8681576.html
Copyright © 2011-2022 走看看