ASP.NET的request、response、responseRedirect、session和application

request
.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpBrowserCapabilities b = Request.Browser;
        Response.Write("客户端浏览器信息:");
        Response.Write("<hr>");
        Response.Write("类型:" + b.Type + "<br>");
        Response.Write("名称:" + b.Browser + "<br>");
        Response.Write("版本:" + b.Version + "<br>");
        Response.Write("操作平台:" + b.Platform + "<br>");
        Response.Write("是否支持框架:" + b.Frames + "<br>");
        Response.Write("是否支持表格:" + b.Tables + "<br>");
        Response.Write("是否支持Cookies:" + b.Cookies + "<br>");
        Response.Write("<hr>");
        Response.Write("客户端其他信息:");
        Response.Write("<hr>");
        Response.Write("客户端主机名称:" + Request.UserHostName + "<br>");
        Response.Write("客户端主机IP:" + Request.UserHostAddress + "<br>");
        Response.Write("指定页面路径:" + Request.MapPath("Default.aspx") + "<br>");
        Response.Write("原始URL:" + Request.RawUrl + "<br>");
        Response.Write("当前请求的URL:" + Request.Url + "<br>");
        Response.Write("客户端HTTP传输方法:" + Request.HttpMethod + "<br>");
        Response.Write("原始用户代理信息:" + Request.UserAgent + "<br>");
        Response.Write("<hr>");
    }
}

Demo:
在这里插入图片描述
response
.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        char c = 'a';
        string s = "Artificial Intelligence";
        char[] cArray = { 'W', 'u', 't' };
        Page p = new Page();
        Response.Write("输出单个字符:");
        Response.Write(c);
        Response.Write("<br>");
        Response.Write("输出一个字符串:" + s + "<br>");
        Response.Write("输出字符数组:");
        Response.Write(cArray, 0, cArray.Length);
        Response.Write("<br>");
        Response.Write("输出一个对象:");
        Response.Write(p);
        Response.Write("<br>");
        Response.Write("输出一个文件:");
        Response.WriteFile(Server.MapPath(@"TextFile.txt"));
    }
}

Demo:
在这里插入图片描述
responseRedirect
1.aspx.cs

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
    <style type="text/css">
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table>
            <tr>
                <td align="right">
                    姓名:</td>
                <td>
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="right">
                    性别:</td>
                <td>
                    <asp:RadioButton ID="rbtnSex1" runat="server"  GroupName="sex" Text="男" />
&nbsp;<asp:RadioButton ID="rbtnSex2" runat="server" GroupName="sex" Text="女" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Button ID="btnOK" runat="server" onclick="btnOK_Click" Text="登录" />
                </td>
            </tr>
        </table>
    
        <br />
    
    </div>
    </form>
</body>
</html>

2.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class welcome : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string name = Request.QueryString["Name"];
        string sex = Request.QueryString["Sex"];
        Response.Write("欢迎" + name + sex + "进入系统!");
    }
}

Demo:
在这里插入图片描述
session和application
1.aspx.cs

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (txtName.Text == "test" && txtPwd.Text == "123")
        {
            Session["UserName"] = txtName.Text;
            Session["TimeLogin"] = DateTime.Now;
            Response.Redirect("~/UserPage.aspx");
        }
        else
        {
            Response.Write("<script>alert('登录失败!请返回查找原因');location='Login.aspx'</script>");
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        txtName.Text = txtPwd.Text = "";
    }
}

2.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("欢迎用户" + Session["UserName"].ToString() + "登录本系统!<br>");
        Label1.Text = "您是该网站的第" + Application["count"].ToString() + "个访问者!";
        Response.Write("您登录的时间为:" + Session["TimeLogin"].ToString());
    }
}

Global.asax

<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        //在应用程序启动时运行的代码

        Application["count"] = 0;

    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //在应用程序关闭时运行的代码

    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        //在出现未处理的错误时运行的代码

    }

    void Session_Start(object sender, EventArgs e) 
    {
        //在新会话启动时运行的代码
        //在新会话启动时运行的代码
        Application.Lock();
        Application["count"] = (int)Application["count"] + 1;
        Application.UnLock();

    }

    void Session_End(object sender, EventArgs e) 
    {
        //在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式 
        //设置为 StateServer 或 SQLServer,则不会引发该事件。
        Application.Lock();
        Application["count"] = (int)Application["count"] - 1;
        Application.UnLock();

    }
</script>

Demo:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值