编程开发 > ASP > 文章内容

利用ASP.NET3.5的路由功能创建搜索引擎友好的URL(6)

2010-10-20编辑:dan

ArticleShow.aspx

 


<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<mce:script runat="server"><!--
protected void Page_Load(object sender, EventArgs e)
{
int ArticleGuid = 0;
HttpContext context = HttpContext.Current;
if (context.Items["ArticleGuid"] != null)
{
Int32.TryParse(context.Items["ArticleGuid"].ToString(), out ArticleGuid);
}
if (ArticleGuid < 1)
{
ArticleTitle.InnerHtml = "加载文章错误";
ArticleContent.InnerHtml = "指定的 标识 无效!";
ArticleContent.Style["color"] = "#f00";
}
else
{
String ConnectionString = "Data Source=(local);Initial Catalog=ArticleDev;User ID=sa;Password=sa";
SqlConnection cn = new SqlConnection(ConnectionString);
cn.Open();
String sql = "Select * From Article Where ArticleId = @ArticleId";
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@ArticleId", ArticleGuid);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
ArticleTitle.InnerHtml = dr["Title"].ToString();
ArticleContent.InnerHtml = dr["Content"].ToString();
}
else
{
ArticleTitle.InnerHtml = "加载文章错误";
ArticleContent.InnerHtml = "指定的文章不存在!";
ArticleContent.Style["color"] = "#f00";
}
cn.Dispose();
}
}
// --></mce:script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<h2 id="ArticleTitle" runat="server" style="text-align:center" mce_style="text-align:center">
</h2>
<hr />
<div id="ArticleContent" runat="server">
</div>
</form>
</body>
</html>

 

这段代码中,我们从自定义路由处理程序中添加的 HttpContext 对象中取出参数 context.Items["ArticleGuid"],然后进行处理。

利用ASP.NET3.5的路由功能创建搜索引擎友好的URL(5)

热点推荐

登录注册
触屏版电脑版网站地图