如何从类文件重定向到页面? [英] How Do I Redirect To A Page From A Class File?

查看:76
本文介绍了如何从类文件重定向到页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public   static  SqlConnection OpenConnection()
{
尝试
{
if (HttpContext.Current.Session [ DATABASE]!= null & ;& HttpContext.Current.Session [ USERNAME]!= null && HttpContext.Current.Session [ PASSWORD] != null && HttpContext.Current.Session [ SERVERNAME]!= null
{
cn = new Sq lConnection( uid = + HttpContext.Current.Session [ USERNAME]。ToString()+ ; pwd = + HttpContext.Current.Session [ PASSWORD] .ToString()+ ; server = + HttpContext.Current.Session [ SERVERNAME]。ToString()+ ; database = + HttpContext.Current.Session [ DATABASE]。ToString()+ );
if (cn.State == ConnectionState.Closed)
{
cn.Open();
}

}
else
{
HttpContext.Current.Response.Redirect( 〜/ Login.aspx);

}
return cn;

}
catch (例外情况)
{
throw new 例外(ex.Message);
}
}







但是,我的Login.aspx在文件夹中管理员.....

应该是什么代码?

解决方案

如果它在根目录中则使用。

 HttpContext.Current.Response.Redirect( 〜/ Login.aspx), 





如果它在任何目录中,则使用目录名称,例如,



 HttpContext.Current.Response.Redirect( 〜/管理员/的Login.aspx 


 HttpContext.Current.Response.Redirect(@ Admin\Login。 aspx); 





试试这个。它重定向到Login.aspx。表格是当地的权利。所以使用 \ 代替 /

另外添加此(可选)

 Response.Flush(); 


public static SqlConnection OpenConnection()
        {
            try
            {
                if (HttpContext.Current.Session["DATABASE"] != null && HttpContext.Current.Session["USERNAME"] != null && HttpContext.Current.Session["PASSWORD"] != null && HttpContext.Current.Session["SERVERNAME"] != null)
                {
                    cn = new SqlConnection("uid=" + HttpContext.Current.Session["USERNAME"].ToString() + ";pwd=" + HttpContext.Current.Session["PASSWORD"].ToString() + ";server=" + HttpContext.Current.Session["SERVERNAME"].ToString() + ";database=" + HttpContext.Current.Session["DATABASE"].ToString() + "");
                    if (cn.State == ConnectionState.Closed)
                    {
                        cn.Open();
                    }

                }
                else
                {
                    HttpContext.Current.Response.Redirect("~/Login.aspx");

                }
                return cn;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }




But, my Login.aspx is in Folder Admin.....
what should be the code for it?

解决方案

use if it is in root directory.

HttpContext.Current.Response.Redirect("~/Login.aspx"),



AND if it is inside any directory, then use directory name like,

HttpContext.Current.Response.Redirect("~/Admin/Login.aspx")


HttpContext.Current.Response.Redirect(@"Admin\Login.aspx");



Try this. Its redirecting to Login.aspx. Forms are in local right. So use \ instead of /.
Also add this (Optional)

Response.Flush();


这篇关于如何从类文件重定向到页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆