asp.net 学习笔记

用WSDL命令可以注册web service
在APS.NET中创建WEB服务
以.ASMX扩展名保存文件

1@ 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文件内容

1<form action=" http://locallhost/WebServiceTest/Service1.asmx/HelloWorld " method="post" name="f1">
2<input name="name" type="test"/><input type="submit"/>
3</form>

Get 调用Web service
在url中传参
如:
http://localhost/WebServiceTest/Service1.asmx?op=HelloWorld&name=MyName

将APSX页面修改为用户控件
去除

 1<html> <body> <form>元素   
 2将Web窗体页中ASP.NET指令类型从@Page更改为@Control   
 3更改指令的CodeBehind属性引用以反映.aspx扩展名将更改为 .ascx   
 4将基类从System.Web.UI.Page更改为System.Web.UI.UserControl 
 5
 6在用户控件中,控件的值可以定义属性   
 7有一个用户控件,如果无法访问的话,可以用FindControl方法   
 8变量=((testControl)this.FindControl("tc")).txtUsername;   
 9//Response.Write(((testControl)this.FindControl("tc")).txtUsername);   
10(testControl)是强制类型转换,括号内是类型   
11FindControl("tc") tc是控件的name   
12.txtUsername是控件的属性 
13
14用户控件的使用(在APSX页面中注册)   

@ Register TagPrefix="uc1" TagName="menu" Src="menu.ascx"

  1TagPrefix 确定用户控件的唯一命名空间,它将是标记中控件名称的前缀   
  2TagName 为用户控件的名称   
  3Src 用户控件的虚拟路径,例如"UserControl1.ascx"   
  4WEB自定义控件 
  5
  6Web.config   
  7<!--   
  8说明:   
  91.所有的配置都必须被放在<configuration>和</configuration>标记之中.   
 102.<appSettings>和</appSettings>之间是自定义配置,通常用来自己设置一些常量,Add添加常量,Key是常量的名称,   
 11value是常量的值.   
 12<appSettings>   
 13<add key="con" value="server=.;database=northwind;uid=sa;pwd=;"></add>   
 14</appSettings>   
 15在程序中可以用System.Configuration.ConfigurationSettings.AppSettings["con"]取值   
 16SqlConnection con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["con"]);   
 17con.Open();   
 18SqlCommand cmd=new SqlCommand("select * from employees",con);   
 19this.DataGrid1.DataSource=cmd.ExecuteReader();   
 20this.DataGrid1.DataBind(); 
 21
 223.<system.web>和</system.web>之间的标记是关于整个应用程序的设置.   
 23如 <pages buffer="true"/> 使用页缓冲   
 244.<location>和</location>是一个区域标记.Path="aaa"表示下面的设置只对该文件有效.   
 25\-->
 26
 27customErrors设置(在<system.web>和</system.web>之间)   
 28语法   
 29<customerrors defaultredirect="url" mode="On|Off|RemoteOnly">
 30<error redirect="url" statuscode="statuscode"></error>
 31</customerrors>   
 32身份验证和授权   
 33身份验证类型: WINDOWS 描述: WINDOWS 身份难作为默认的身份验证模式.用于任何形式的IIS身份验证   
 34身份验证类型: FORMS 描述: 基于APS.NET窗体的身份验证作为默认的身份验证模式   
 35身份验证类型: PASSPORT 描述:Microsoft Passport身份验证作为默认的身份验证模式   
 36身份验证类型: NONE 描述: 没有身份验证.用于匿名用户和可以提供其自己的身份验证的应用程序.   
 37<configuration>
 38<system.web>
 39<authentication mode="Windows|Forms|Passport|None">?   
 40<forms loginurl="url" name="name" path="/" protection="All|NOne|Encryption" timeout="xx">?   
 41<credentials passwordformat="Clear|SHA1|MD5"> /*Clear为明文密码*/   
 42<user name="用户名" password="密码"></user>
 43</credentials>
 44</forms>?   
 45<passport redirecturl="internal"></passport>?   
 46</authentication>
 47</system.web>
 48</configuration>
 49
 50//基于forms先把IIS中该应用的匿名访问去掉   
 51<forms>标记的属性   
 52属性 选项 描述   
 53name None 登录身份验证的Cookie名称   
 54loginUrl None 登录页URL.如果没有身份验证Cookie,客户端将被重定向到此URL   
 55protection ALL 应用程序同时使用数据验证和加密来保护Cookie   
 56None 加密和验证都禁用   
 57timeout 一段时间(按分钟计),这段时间之后身份验证Cookie将到期,默认值为30   
 58path 由应用程序发布的Cookie的路径.默认值是反斜杠(/) 
 59
 60<authentication mode="Forms">
 61<forms loginurl="login.aspx" name="YourCookieName" protection="ALL"></forms>
 62</authentication>   
 63//授权   
 64<authorization>
 65<allow users="?"></allow> //<allow users="*"></allow><!--允许所有用户 -->
 66<!--   
 67<allow users="[逗号分隔的用户列表]"   
 68roles="[逗号分隔的角色列表]"/>   
 69<deny users="[逗号分隔的用户列表]"   
 70roles="[逗号分隔的角色列表]"/>   
 71\-->
 72</authorization>
 73
 74//login.aspx   
 75登录代码   
 76//连接数据库进行验证   
 77if(true)//用户名是否合法   
 78{   
 79// System.Web.Security.FormsAuthentication.SetAuthCookie(this.TextBox1.Text,false);//指定的用户名写入到Cookie中(false临时Cookie,true永久Cookie)   
 80// Response.Redirect("");   
 81System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.TextBox1.Text,false);//转到用户原访问的页   
 82//如果为true,可用System..Web.Security.FormsAuthentication.SignOut();删除,以后又要求登录   
 83}   
 84else   
 85{   
 86Response.Write("用户不合法");   
 87} 
 88
 89//如果用户少的话,可以不用数据库,直接允许/拒绝用户   
 90<authentication <forms="" loginurl="login.aspx" mode="Forms" name="authCre" protection="ALL">
 91<credentials passwordformat="Clear">
 92<user name="aaa" password="aaa"></user>
 93<user name="bbb" password="bbb"></user>
 94</credentials>
 95</authentication></forms>
 96   
 97登录代码   
 98private void Button1_Click(object sender,System.EventArgs e)   
 99{   
100if(System.Web.Security.FormsAuthentication.Authenticate(this.TextBox1.Text,This.TextBox2.Text)   
101{   
102// System.Web.Security.FormsAuthentication.SetAuthCookie(this.TextBox1.Text,true);//此方法需重定向   
103// Response.Redirect("WebForm1.aspx");   
104System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.TextBox1.Text,false);//此方法不需要重定向,直接转到原访问页   
105}   
106else   
107{   
108Response.Write("用户不合法");   
109}   
110}   
111//授权时,通配符*表示任何人,?表示匿名</form></body></html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus