作者:林亚培_724 | 来源:互联网 | 2014-05-05 09:01
通过创立WEBServer代理可以当作本地类应用,但能不能返回指定的XML呢?比如通过checkpass服务检测帐号和密码之后要返回该用户拥有的权限列表。怎么实现呢?ASP.NETWeb服务支撑在公共语
通过创立WEBServer代理可以当作本地类应用,但能不能返回指定的XML呢?比如通过checkpass服务检测帐号和密码之后要返回该用户拥有的权限列表。怎么实现呢?
ASP.NET Web服务支撑在公共语言运行时中支撑的所有基础数据类型,包含String,integer,Long等等。除了简略的基础数据类型之外,还支撑基础数据类型的数组。
但是,更有趣的是支撑用户定义的类和结构体。基础上,任何可由XSD模式代表的类型都是可以作为ASP.NET的参数或返回类型。
通过一个星期的摸索,解决了这个标题,并学习了如何读取和输出XML文档;数据库把持;WebServer的创立和引用。下面就部分源码供初学习者参考,不足之此请指正。
/*CheckLogin服务*/
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using mysql.SQL;
using myfunc.Common;
///
/// CheckLogin 的摘要阐明
///
[WebService(Namespace = "http://localhost/")]
[WebServiceBinding(COnformsTo= WsiProfiles.BasicProfile1_1)]
public class CheckLogin : System.Web.Services.WebService {
public CheckLogin () {
//假如应用设计的组件,请取消注释以下行
//InitializeComponent();
}
//[WebMethod(Description = "Login", EnableSession = true)]
[WebMethod]
public checkuser Login(string sUserCode, string sPassword)
{
checkuser objcheckuser= new checkuser();
string sCheckLogin = ConfigurationManager.AppSettings["strCheckLogin"];
SqlShell objShell = new SqlShell();
SqlCommand objCommand = new SqlCommand(sCheckLogin);
objCommand.CommandType = CommandType.Text;
objCommand.Parameters.AddWithValue("@sUserCode", sUserCode);
objCommand.Parameters.AddWithValue("@sPassword", sPassword);
DataTable objDataTable = objShell.executeDataSet(ref objCommand).Tables[0];
objcheckuser.logined = (objDataTable.Rows.Count > 0);
if (objcheckuser.logined)
{
//帐号和密码准确,反回帐号信息
DataRow objDataRow = objDataTable.Rows[0];
objcheckuser.userid = objDataRow["UserID"].ToString().Trim(); ;
objcheckuser.pass = objDataRow["Pass"].ToString().Trim();
objcheckuser.username = objDataRow["UserName"].ToString().Trim();
//检查Allow字段是否为空
if (objDataRow.IsNull("Allow")) { objcheckuser.allow = ""; }
else { objcheckuser.allow = objDataRow["Allow"].ToString().Trim(); }
menulist objmenulist = new menulist(objDataRow["UserID"].ToString().Trim());
objcheckuser.menuxml = objmenulist.buf;//返回菜单列表的XML字符串
}
return objcheckuser;
}
public class checkuser
{
public bool logined;
public string userid;
public string pass;
public string username;
public string allow;
public string menuxml;//返回菜单列表的XML字符串
}
}
/*CheckLogin服务结束*/
/*menulist 类开端*/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using Singcn.SQL;
using System.Data.SqlClient;
using System.IO;
using System.Text;
namespace myfunc.Common
{
///
/// PubFunc 的摘要阐明
///
public class menulist
{
public XmlWriterSettings settings = new XmlWriterSettings();
public XmlWriter writer = null;
public string buf = "";
public SqlShell objShell;
public SqlCommand objCommand;
public DataTable objDataTable;
public menulist(string userid)
{
objShell = new SqlShell();
objCommand = new SqlCommand("select * from qxdmb order by jb,px,qxdm");
objCommand.CommandType = CommandType.Text;
objDataTable = objShell.executeDataSet(ref objCommand).Tables[0];
StringWriter writerstr = new StringWriter();
settings.Indent = true;
settings.Encoding = Encoding.GetEncoding("utf-8");
try
{
writer = XmlWriter.Create(writerstr, settings);
writer.WriteStartDocument();
writer.WriteStartElement("DSTreeRoot");
writer.WriteAttributeString("text", "后台治理系统-[" userid "]");