asp.net学习笔记
用WSDL命令可以注册web service
在APS.NET中创建WEB服务
以.ASMX扩展名保存文件
<%@ WebService Language="c#" class="TestWS" %
using System.Web.Services;
class TestWS
{
[WebMethod]
public string SayHello(string name)
{
return "Hello"+name;
}
}
POST 调用Web service
//以下为a.html文件内容
<form name="f1" method="post" action="http://locallhost/WebServiceTest/Service1.asmx/HelloWorld"
<input type="test" name="name"<input type="submit"
</form
Get 调用Web service
在url中传参
如:
http://localhost/WebServiceTest/Service1.asmx?op=HelloWorld&name=MyName
将APSX页面修改为用户控件
去除<html <body <form元素
将Web窗体页中ASP.NET指令类型从@Page更改为@Control
更改指令的CodeBehind属性引用以反映.aspx扩展名将更改为 .ascx
将基类从System.Web.UI.Page更改为System.Web.UI.UserControl
在用户控件中,控件的值可以定义属性
有一个用户控件,如果无法访问的话,可以用FindControl方法
变量=((testControl)this.FindControl("tc")).txtUsername;
//Response.Write(((testControl)this.FindControl("tc")).txtUsername);
(testControl)是强制类型转换,括号内是类型
FindControl("tc") tc是控件的name
.txtUsername是控件的属性
用户控件的使用(在APSX页面中注册)
<%@ Register TagPrefix="uc1" TagName="menu" Src="menu.ascx" %
TagPrefix 确定用户控件的唯一命名空间,它将是标记中控件名称的前缀
TagName 为用户控件的名称
Src 用户控件的虚拟路径,例如"UserControl1.ascx"
WEB自定义控件
Web.config
<!--
说明:
1.所有的配置都必须被放在<configuration和</configuration标记之中.
2.<appSettings和</appSettings之间是自定义配置,通常用来自己设置一些常量,Add添加常量,Key是常量的名称,
value是常量的值.
<appSettings
<add key="con" value="server=.;database=northwind;uid=sa;pwd=;"</add
</appSettings
在程序中可以用System.Configuration.ConfigurationSettings.AppSettings["con"]取值
SqlConnection con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["con"]);
con.Open();
SqlCommand cmd=new SqlCommand("select * from employees",con);
this.DataGrid1.DataSource=cmd.ExecuteReader();
this.DataGrid1.DataBind();
3.<system.web和</system.web之间的标记是关于整个应用程序的设置.
如 <pages buffer="true"/ 使用页缓冲
4.<location和</location是一个区域标记.Path="aaa"表示下面的设置只对该文件有效.
--
customErrors设置(在<system.web和</system.web之间)
语法
<customErrors
defaultRedirect="url"
mode="On|Off|RemoteOnly"
<error statusCode="statuscode" redirect="url"/
</customErrors
身份验证和授权
身份验证类型: WINDOWS 描述: WINDOWS 身份难作为默认的身份验证模式.用于任何形式的IIS身份验证
身份验证类型: FORMS 描述: 基于APS.NET窗体的身份验证作为默认的身份验证模式
身份验证类型: PASSPORT 描述:Microsoft Passport身份验证作为默认的身份验证模式
身份验证类型: NONE 描述: 没有身份验证.用于匿名用户和可以提供其自己的身份验证的应用程序.
<configuration
<system.web
<authentication mode="Windows|Forms|Passport|None"?
<forms name="name" loginUrl="url"
protection="All|NOne|Encryption"
timeout="xx" path="/"?
<credentials passwordFormat="Clear|SHA1|MD5" /*Clear为明文密码*/
<user name="用户名" password="密码"/
</credentials
</forms?
<passport redirectUrl="internal"/?
</authentication
</system.web
</configuration
//基于forms先把IIS中该应用的匿名访问去掉
<forms标记的属性
属性
选项
描述
name
None
登录身份验证的Cookie名称
loginUrl
None
登录页URL.如果没有身份验证Cookie,客户端将被重定向到此URL
protection ALL
应用程序同时使用数据验证和加密来保护Cookie
None
加密和验证都禁用
timeout
一段时间(按分钟计),这段时间之后身份验证Cookie将到期,默认值为30
path
由应用程序发布的Cookie的路径.默认值是反斜杠(/)
<authentication mode="Forms"
<forms name="YourCookieName" loginUrl="login.aspx" protection="ALL"</forms
</authentication
//授权
<authorization
<allow users="?"/
//<allow users="*"/<!--允许所有用户 --
<!--
<allow users="[逗号分隔的用户列表]"
roles="[逗号分隔的角色列表]"/
<deny users="[逗号分隔的用户列表]"
roles="[逗号分隔的角色列表]"/
--
</authorization
//login.aspx
登录代码
//连接数据库进行验证
if(true)//用户名是否合法
{
// System.Web.Security.FormsAuthentication.SetAuthCookie(this.TextBox1.Text,false);//指定的用户名写入到Cookie中(false临时Cookie,true永久Cookie)
// Response.Redirect("");
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.TextBox1.Text,false);//转到用户原访问的页
//如果为true,可用System..Web.Security.FormsAuthentication.SignOut();删除,以后又要求登录
}
else
{
Response.Write("用户不合法");
}
//如果用户少的话,可以不用数据库,直接允许/拒绝用户
<authentication mode="Forms"
<forms name="authCre" loginUrl="login.aspx" protection="ALL"
<credentials passwordFormat="Clear"
<user name="aaa" password="aaa"/
<user name="bbb" password="bbb"/
</credentials
</forms
</authentication
登录代码
private void Button1_Click(object sender,System.EventArgs e)
{
if(System.Web.Security.FormsAuthentication.Authenticate(this.TextBox1.Text,This.TextBox2.Text)
{
//
System.Web.Security.FormsAuthentication.SetAuthCookie(this.TextBox1.Text,true);//此方法需重定向
//
Response.Redirect("WebForm1.aspx");
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.TextBox1.Text,false);//此方法不需要重定向,直接转到原访问页
}
else
{
Response.Write("用户不合法");
}
}
//授权时,通配符*表示任何人,?表示匿名