读书人

asp.net 在App_Code的类中怎么使用物

发布时间: 2012-05-21 18:04:41 作者: rapoo

asp.net 在App_Code的类中,如何使用物理路径??
我想对每个页面都判断一下是否登陆,网上找的方法是加一个BasePage类,然后再加到每个页面上,但是总是404错误,求解决办法??
BasePage类:

C# code
    public class BasePage : System.Web.UI.Page    {        public BasePage()        {            this.Load += new EventHandler(BasePage_Load);         }        void BasePage_Load(object sender, EventArgs e)        {            if (Session["username"] == null||Session["username"].ToString().Equals(" "))            {                Response.Write("<script>alert('请先登录!');window.location='../Default.aspx';</script>");            }        }     }

在调用页面的CS中写了:
C# code
public partial class _Default : BasePage {     protected void Page_Load(object sender, EventArgs e)     {        ..........    } }

怎么办呢???

[解决办法]
System.Web.HttpContext.Current.Response.Write("<script>alert('请先登录!');window.location='" + VirtualPathUtility.ToAbsolute("~/") + "Default.aspx';</script>");

或者
System.Web.HttpContext.Current.Response.Write("<script>alert('请先登录!');window.location='" + Page.ResolveUrl("~/") + "Default.aspx';</script>");

读书人网 >asp.net

热点推荐