ASP.Net元标记:从数据库中以编程方式填充页眉部分中的标题,关键字和描述

时间:2017-01-10 06:27:41

标签: c# asp.net sql-server url-rewriting asp.net-ajax

我正在尝试在我的ASP.net应用程序中创建搜索选项,以根据位置检查医生详细信息。如果我尝试以下代码,则只显示主页。 http://example.com/Metatag/Home.aspx。我希望根据位置更改URL。预期网址示例:http://example.com/Metatag/chennai/doctors/saidapet

我是这项技术的新手。

页面加载

protected void Page_Load(object sender, EventArgs e)
{
     string page = Request.Url.Segments[Request.Url.Segments.Length - 1];
     string location = Request.Url.Segments[Request.Url.Segments.Length - 1];
     DataTable dtMeta = this.GetData(page,location);

     //Add Page Title
     this.Page.Title = dtMeta.Rows[0]["Title"].ToString();

     //Add Keywords Meta Tag
     HtmlMeta keywords = new HtmlMeta();
     keywords.HttpEquiv = "keywords";
     keywords.Name = "keywords";
     keywords.Content = dtMeta.Rows[0]["Keywords"].ToString();
     this.Page.Header.Controls.Add(keywords);

     //Add Description Meta Tag
     HtmlMeta description = new HtmlMeta();
     description.HttpEquiv = "description";
     description.Name = "description";
     description.Content = dtMeta.Rows[0]["Description"].ToString();
     this.Page.Header.Controls.Add(description);
 }

GetData Table

private DataTable GetData(string page,string location)
{
    string query = "SELECT Title, Description, Keywords FROM MetaTags WHERE LOWER(Page,Location) = LOWER(@Page,@Location)";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(query))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@Page", page);
                cmd.Parameters.AddWithValue("@Location", location);
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                DataTable dt = new DataTable();
                sda.Fill(dt);
                return dt;
            }
        }
    }
}

数据库:

Get url with location

我在尝试this code

1 个答案:

答案 0 :(得分:2)

使用Page.MetaKeywords

Page.Title = "Your Page Title"; Page.MetaDescription = "Your Page Description"; Page.MetaKeywords = "Your Page Keywords";
List<T>

使用this link

进行网址重写