为在 Asp.Net C# 中单击的所有帖子动态生成友好 URL 并在唯一页面中呈现来自 SQL 数据库的内容

Dynamically generating friendly URL and render the content from SQL db in unique page for all posts clicked in Asp.Net C#

当我在我的网站上构建博客部分时,我想知道如何在独特的页面模板或视图中呈现每个博客 post。我不想手动创建 aspx 然后用标题 SEO 命名它们。

第 1 步:我创建了一个 table,它可以完全存储所有详细信息,包括图像、内容、标题,并且我能够检索这些值以将其进一步绑定到转发器中。工作正常

第2步:但是,现在接下来的事情是,点击任何一个博客都会显示博客详细信息页面,但是它会带有与标题匹配的不同URL吗?

假设当前 URL 是 www.xyz.com/blog/blogdeatils.aspx,如果它正在呈现一篇谈论维基百科的文章,那么我希望我的 URL 是 www.xyz.com/blog/how-wikipedia-works(无 aspx 扩展名)

这可能吗?谢谢大家

这被称为 "routing"。将 Global.asax 添加到您的项目中,接下来只是您的文件示例:

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>


<script runat="server">

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    RegisterRoutes(RouteTable.Routes);

}

void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("Article", "blog/{name}", "~/BlogDetails.aspx");
}

void Application_End(object sender, EventArgs e) 
{
    //  Code that runs on application shutdown

}

void Application_Error(object sender, EventArgs e) 
{ 
    // Code that runs when an unhandled error occurs

}

void Session_Start(object sender, EventArgs e) 
{
    // Code that runs when a new session is started

}

void Session_End(object sender, EventArgs e) 
{
    // Code that runs when a session ends. 
    // Note: The Session_End event is raised only when the sessionstate mode
    // is set to InProc in the Web.config file. If session mode is set to StateServer 
    // or SQLServer, the event is not raised.

}

并且在您的 BlogDetails.aspx 中,您可以使用下一个代码来获取文章的标题:

string article_name = Page.RouteData.Values["name"] == null ? "No article" : Page.RouteData.Values["name"].ToString();

然后使用它从数据库中检索信息。

您的Global.ascx文件内容:

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>


<script runat="server">

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    RegisterRoutes(RouteTable.Routes);
}

void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("Article", "blog/{name}", "~/BlogDetails.aspx");
    routes.MapPageRoute("Main Page", "Home", "~/Default.aspx");
}

void Application_End(object sender, EventArgs e) 
{
    //  Code that runs on application shutdown
}

void Application_Error(object sender, EventArgs e) 
{ 
    // Code that runs when an unhandled error occurs
}

void Session_Start(object sender, EventArgs e) 
{
    // Code that runs when a new session is started
}

void Session_End(object sender, EventArgs e) 
{
    // Code that runs when a session ends. 
    // Note: The Session_End event is raised only when the sessionstate mode
    // is set to InProc in the Web.config file. If session mode is set to StateServer 
    // or SQLServer, the event is not raised.
}

假设您有一些 table 文章,其中包含 ID、名称、图像、信息 等列。在 BindDetails.aspx[=31 中,您对图像有 Image 控件,对详细信息有 Label 控件=].所以你的 BindDetails.aspx.cs 将是(命名空间除外):

protected void Page_Load(object sender, EventArgs e){
    if (Page.RouteData.Values["name"] == null)
        Response.Redirect("~/Home");
    else{
        string name = Page.RouteData.Values["name"].ToString();
        if (!IsPostBack)
            RetrieveArticleByItsName(name);
    }
}

protected void RetrieveArticleByItsName(string article){
    SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["YourConnectionName"].ConnectionString;
    SqlCommand cmd = new SqlCommand("select TOP 1 * from Articles where lower(name) = lower(@name)", con);
    cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = article;

    try{
        con.Open();
        SqlDataReader sdr = cmd.ExecuteReader();
        sdr.Read();
        imgImage.ImageUrl = "your_folder/" + sdr["image"].ToString();
        lblDetails.Text = sdr["info"].ToString();
    }
    catch(Exception ex){
        //work with exceptions
        Response.Write("<script language='javascript'>alert('" + ex.Message.ToString() + "');</script"); // Get alert with exception
    }
    finally{
        con.Close();
    }
}

假设您有两篇文章,名称分别为“How-to-clean-home”和“How-to-clean-a-汽车”。现在你可以给每个人链接 xyz.com/blog/How-to-clean-homexyz.com/blog/How-to-clean-a-car 并且它会起作用。

希望对您有所帮助。