创建一个示例和WebMethod特性解析
系列文章导航:
现在开始学习Web服务,写下自己学习过程中的一些感受,拿出来与大家分享,欢迎批评指正。希望能给跟我一样初学Web服务的朋友一些帮助。
Web服务的宗旨是创建不需要用户界面就能与其他应用程序交互的Web应用程序。它是松耦合的,并与服务器端和客户端所使用的操作系统、编程语言都无关,这样就在一定的情况下就会使你的效率大大提高。但创建Web服务必须保证的是,服务器端和客户端都要支持行业标准协议HTTP、SOAP(simple object access protocal 简单对象访问协议)和XML。
创建Web服务的过程:
(1):打开VS,创建一个ASP.NET WEB服务应用程序;
(2):输入下列代码,这是一个有关学生信息的二维数组和几个方法
//构造并填充info数组
string[,] strStuInfo ={
{"200511020120","贰零零五届","人文法律学院","社会工作","(1)班","无悔","1"},
{"200511020121","贰零零五届","人文法律学院","社会工作","(1)班","梁需","2"},
{"200511010122","贰零零五届","人文法律学院","法律专业","(1)班","陆磊","3"},
{"200511010220","贰零零五届","人文法律学院","法律专业","(2)班","白灵","4"},
{"200511010221","贰零零五届","人文法律学院","法律专业","(2)班","剑付","5"},
{"200511020222","贰零零五届","人文法律学院","社会工作","(2)班","敬意","6"},
{"200511100120","贰零零五届","化工生物学院","生物制药","(1)班","黄兴","7"},
{"200511100221","贰零零五届","化工生物学院","生物制药","(2)班","蕾蕾","8"},
{"200511100322","贰零零五届","化工生物学院","生物制药","(3)班","白冰","9"},
{"200611120120","贰零零六届","化工生物学院","发酵工程","(1)班","书并","10"},
{"200611120121","贰零零六届","化工生物学院","发酵工程","(1)班","小鱼","11"},
{"200611120421","贰零零六届","化工生物学院","发酵工程","(4)班","声声","12"},
};
[WebMethod(Description = "根据学号获得学生的姓名")]
public string GetName(string strStuNum)
{
for (int i = 0; i < strStuInfo.GetLength(0); i++)
{
if (String.Compare(strStuNum, strStuInfo[i, 0], true) == 0)
return strStuInfo[i, 5].ToString();
}
return "您输入的学号不存在";
}
[WebMethod(Description = "根据学号获得学生的专业和班级")]
public string GetClass(string strStuNum)
{
for (int i = 0; i < strStuInfo.GetLength(0); i++)
{
if (String.Compare(strStuNum, strStuInfo[i, 0], true) == 0)
{
return strStuInfo[i, 3].ToString() + strStuInfo[i, 4].ToString();
}
}
return "您输入的学号不存在";
}
[WebMethod(Description = "根据学号获得学生的入学时间")]
public string GetYear(string strStuNum)
{
for (int i = 0; i < strStuInfo.GetLength(0); i++)
{
if (String.Compare(strStuNum, strStuInfo[i, 0], true) == 0)
{
return strStuInfo[i, 1].ToString();
}
}
return "您输入的学号不存在";
}
[WebMethod(Description = "根据学号获得学生的学院"]
public string GetCollege(string strStuNum)
{
for (int i = 0; i < strStuInfo.GetLength(0); i++)
{
if (String.Compare(strStuNum, strStuInfo[i, 0], true) == 0)
{
return strStuInfo[i, 2].ToString();
}
}
return "您输入的学号不存在";
}
[WebMethod(Description = "点击次数", EnableSession = true)]
public int HitCounter()
{
if (Session["HitCounter"] == null)
{
Session["HitCounter"] = 1;
}
else
{
Session["HitCounter"] = ((int)Session["HitCounter"]) + 1;
}
return ((int)Session["HitCounter"]);
}
string[,] strStuInfo ={
{"200511020120","贰零零五届","人文法律学院","社会工作","(1)班","无悔","1"},
{"200511020121","贰零零五届","人文法律学院","社会工作","(1)班","梁需","2"},
{"200511010122","贰零零五届","人文法律学院","法律专业","(1)班","陆磊","3"},
{"200511010220","贰零零五届","人文法律学院","法律专业","(2)班","白灵","4"},
{"200511010221","贰零零五届","人文法律学院","法律专业","(2)班","剑付","5"},
{"200511020222","贰零零五届","人文法律学院","社会工作","(2)班","敬意","6"},
{"200511100120","贰零零五届","化工生物学院","生物制药","(1)班","黄兴","7"},
{"200511100221","贰零零五届","化工生物学院","生物制药","(2)班","蕾蕾","8"},
{"200511100322","贰零零五届","化工生物学院","生物制药","(3)班","白冰","9"},
{"200611120120","贰零零六届","化工生物学院","发酵工程","(1)班","书并","10"},
{"200611120121","贰零零六届","化工生物学院","发酵工程","(1)班","小鱼","11"},
{"200611120421","贰零零六届","化工生物学院","发酵工程","(4)班","声声","12"},
};
[WebMethod(Description = "根据学号获得学生的姓名")]
public string GetName(string strStuNum)
{
for (int i = 0; i < strStuInfo.GetLength(0); i++)
{
if (String.Compare(strStuNum, strStuInfo[i, 0], true) == 0)
return strStuInfo[i, 5].ToString();
}
return "您输入的学号不存在";
}
[WebMethod(Description = "根据学号获得学生的专业和班级")]
public string GetClass(string strStuNum)
{
for (int i = 0; i < strStuInfo.GetLength(0); i++)
{
if (String.Compare(strStuNum, strStuInfo[i, 0], true) == 0)
{
return strStuInfo[i, 3].ToString() + strStuInfo[i, 4].ToString();
}
}
return "您输入的学号不存在";
}
[WebMethod(Description = "根据学号获得学生的入学时间")]
public string GetYear(string strStuNum)
{
for (int i = 0; i < strStuInfo.GetLength(0); i++)
{
if (String.Compare(strStuNum, strStuInfo[i, 0], true) == 0)
{
return strStuInfo[i, 1].ToString();
}
}
return "您输入的学号不存在";
}
[WebMethod(Description = "根据学号获得学生的学院"]
public string GetCollege(string strStuNum)
{
for (int i = 0; i < strStuInfo.GetLength(0); i++)
{
if (String.Compare(strStuNum, strStuInfo[i, 0], true) == 0)
{
return strStuInfo[i, 2].ToString();
}
}
return "您输入的学号不存在";
}
[WebMethod(Description = "点击次数", EnableSession = true)]
public int HitCounter()
{
if (Session["HitCounter"] == null)
{
Session["HitCounter"] = 1;
}
else
{
Session["HitCounter"] = ((int)Session["HitCounter"]) + 1;
}
return ((int)Session["HitCounter"]);
}