启明星门户网站Portal发布V4.5,并兼论部分功能的实现

下载地址 http://www.dotnetcms.org/download/portal/portal4.5.0.0.rar

今天发布了启明星Portal系统V4.5.0.0,其实我在测试时,已经发现了部分错误,因为有一些客户一直催着快的做,也就在“大干快上”的思想上,先发布了。

在这里,我们并没有公开源代码,但是通过对部分知识点的介绍,你还是能够了解部分技术的实现。

1。换肤

换肤的方法有很多,一般这里有两种:

1)使用用户控件,类似博客园

例如在有一个default.aspx页面,现在有红色和蓝色两种主题,位置分别为 red/index.ascx 和blue/index.ascx

那么在default.aspx里使用

Control c =Page.LoadControl(ascxfile)
Page.Control.Add(c);

 通过修改index.ascx即可动态实现换肤。

2)使用master

在我们的Portal系统里,除了使用第一种外,还使用了动态更改master的方式。代码示意如下:在Page的Init事件里更改页面的主题。

    protected void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "master.master";

}

当然,如果仅仅那么做,也只是更改页面的布局,例如将table的行由2换成3,但是table的样式,例如字体是红色,在更换master后,仍然是红色,所以,还需要修改页面的css,这里使用了web.config里theme 属性。也就是,:

    protected void Page_PreInit(object sender, EventArgs e)
{
if (Page.Theme == null)
{
this.MasterPageFile = "default.master";
}
else
{
this.MasterPageFile = Page.Theme+".master"
}

}

这样当用户修改web.config里 theme时,其母版跟着变动,同时引用的CSS也同样跟着变动。

 

2.天气预报

 在Portal系统里,使用了天气预报,下面列出了天气预报的代码,天气预报每隔30分钟读取一次,读取后,会写入app_data下的weather.xml里,

    public static  string getWeather()
{
string file=HttpContext.Current.Server.MapPath("~/app_data/weather.txt");
DateTime dt1
= DateTime.Now;
DateTime dt2
= DateTime.Now;
bool isexists = false;
if (File.Exists(file))
{
dt2
= File.GetLastWriteTime(file);
isexists
= true;
}
else
{
isexists
= false;
File.Create(file);
}


if ( (dt1.Subtract(dt2).Minutes >30)||(!isexists))
{
//每隔30小时读取一次
DataTable dt = Helper.ReadConfigXml("~/app_data/weather.xml");
bool userproxy = Helper.ReadConfigValue(dt, "isuseproxy").ToString() == "1" ? true : false;
string ip = Helper.ReadConfigValue(dt, "ip").ToString();
int port = int.Parse(Helper.ReadConfigValue(dt, "port").ToString());
string proxyusername = Helper.ReadConfigValue(dt, "proxyusername").ToString();
string proxypassword = Helper.ReadConfigValue(dt, "proxypassword").ToString();
string url = Helper.ReadConfigValue(dt, "url").ToString();
string responseFromServer = "";
if (userproxy)
{

WebProxy myProxy
= new WebProxy(ip, port);
myProxy.Credentials
= new NetworkCredential(proxyusername, proxypassword);
myProxy.BypassProxyOnLocal
= true;
WebRequest.DefaultWebProxy
= myProxy;
WebRequest myWebRequest
= WebRequest.Create(url);
myWebRequest.Timeout
= 60000;

HttpWebResponse response
= (HttpWebResponse)myWebRequest.GetResponse();

Stream dataStream
= response.GetResponseStream();
StreamReader reader
= new StreamReader(dataStream);
responseFromServer
= reader.ReadToEnd();

reader.Close();
dataStream.Close();
response.Close();

}
else
{
WebRequest request
= WebRequest.Create(url);
request.Credentials
= CredentialCache.DefaultCredentials;
HttpWebResponse response
= (HttpWebResponse)request.GetResponse();
Stream dataStream
= response.GetResponseStream();
StreamReader reader
= new StreamReader(dataStream);
responseFromServer
= reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();

}

File.WriteAllText(file, responseFromServer);

}

return File.ReadAllText(file);



}

对天气预报的访问,需要访问互联网,部分公司是使用代理的,在app_data下有一个weather.xml文件,用来配置天气预报的信息。上面的程序如果你需要直接使用,可以将proxyusername ,proxypassword等修改为你字符串变量就可以了。

在天气预报的后台(也就是weather.aspx.cs里)的Page_Load事件代码是:

  protected void Page_Load(object sender, EventArgs e)
{
string a= WeatherHelper.getWeather();
Page.RegisterClientScriptBlock(
"data", "<script> var jsData=" + a + "</script>");

}

 然后再前台就可以调用了 (天气预报调用地址:http://m.weather.com.cn/data/101020100.html

  <div class="weather">
<ul>
<li>
<img id="wdicon1" src="../Images/Weather/blank.gif" align="absmiddle" style="padding-right:2px" alt=""/>
<span id="wdContent1"></span>
</li>
<li style="padding-left:10px">
<img id="wdicon2" src="../Images/Weather/blank.gif" align="absmiddle" style="padding-right:2px" alt="" />
<span id="wdContent2"></span>
</li>
</ul>

</div>

<script>

document.getElementById(
'wdContent1').innerHTML='今日' + jsData.weatherinfo.img_title1 + ' ' + jsData.weatherinfo.temp1.replace('~', '/').replace('', '');
document.getElementById(
'wdContent2').innerHTML='明日' + jsData.weatherinfo.img_title3 + ' ' + jsData.weatherinfo.temp2.replace('~', '/').replace('', '');
document.getElementById(
'wdicon1').src='../Images/Weather/d' + jsData.weatherinfo.img_title1 + '.gif';
document.getElementById(
'wdicon2').src='../Images/Weather/d' + jsData.weatherinfo.img_title3 + '.gif';
document.getElementById(
'wdicon1').alt=jsData.weatherinfo.weather1;
document.getElementById(
'wdicon2').alt=jsData.weatherinfo.weather2;
</script>

成功后的效果如下:(具体参考下载包里效果)

3.关于首页右边图片滚动切换

在Portal首页右边,有一个图片滚动切换,这里使用了MSClass (具体地址是 http://www.popub.net/script/MSClass.html 这是一个很不错的JS插件)

推荐一下。

4.关于Cookie

Cookie是一个老生常谈的问题,在我的网站里,除了Portal系统还有预定系统Book,Helpdesk系统,当初为了方便,将用户登录做在了一个模块里,例如用户输入

http://localhost/portal

http://localhost/helpdesk

http://localhost/book

就可以访问这3个系统,但是他们的cookie名称都是username,也就是在登录时,代码类似如下:

       HttpContext.Current.Response.Cookies["userinfo"]["userid"] = uid.ToString();
HttpContext.Current.Response.Cookies["userinfo"]["username"] = HttpContext.Current.Server.UrlEncode(dr["username"].ToString());
HttpContext.Current.Response.Cookies["userinfo"].Expires = DateTime.Now.AddDays(2);

这样,当用户退出一个系统时,全部退出,当登录一个系统时,全部登录,(默认的cookie path是“/”)

 还无疑问,只能够通过Application path来区分。

因此增加了如下代码

  HttpContext.Current.Response.Cookies["userinfo"].Path = path;

原本以为这样就可以了,但是在使用身份验证票时出现了问题,我们可以看看MSDN里

  FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, i.ToString(), DateTime.Now, DateTime.Now.AddDays(7), true, Roles,cookiePath);

此处的CookiePath是一个动态的,通过设置其值就可以。上面代码看起来很好,但是用户在退出时,用SignOut方法时

FormsAuthentication.SignOut()

发现无效,最终只能够使用如下列来清除身份验证票。

                HttpContext.Current.Response.Cookies[FormsAuthentication.FormsCookieName].Path = path;
HttpContext.Current.Response.Cookies[FormsAuthentication.FormsCookieName].Expires = DateTime.Now.AddDays(-1);

关于其他分享,后续会陆续介绍,欢迎拍砖 (*^__^*) 嘻嘻……

下载地址 http://www.dotnetcms.org/download/portal/portal4.5.0.0.rar

演示 http://demo.dotnetcms.org

我的网站 http://www.dotnetcms.org 

原文地址:https://www.cnblogs.com/mqingqing123/p/2121198.html