Creating blog

Donald Symmons 2,856 Reputation points
2022-12-29T16:22:11.377+00:00

I want to learn how to create a blog. I created 3 webform to take care of this. The first webform is used to insert data into the database, the second web form is used to display the Titles and the third webform is to display the Titles, and contents of the blog. I named the webforms (addblog.aspx, blogTitles.aspx, and blogcontents.aspx).
I am not going into the part where I insert the data because that is working.
I am having issue with the part where I click on a blog Title to redirect to the page where the contents will be displayed, but I get an error that states:

"The resource you are looking for has been removed, had its name changed ..........". I don't know if I did not get to write the routing well in the Global.asax file. Please help me check. Thanks

This is the HTML Markup for blogTitles.aspx where blog Titles will be displayed. Here, when you click on the Hyperlink used to display the titles it should redirect and display the contents

 <div class="col-sm-10" style="margin: 0 auto; padding: 6px;">  
                    <asp:Repeater ID="rptPages" runat="server">  
                        <ItemTemplate>  
                            <%# Container.ItemIndex + 1 %>.&nbsp;  
                <asp:HyperLink ID="HyperLink2" NavigateUrl='<%# string.Format("~/contents/{0}/{1}.aspx", Eval("ArticleId"), Eval("Slug")) %>'  
                    Text='<%# Eval("Title") %>' runat="server" />  
                        </ItemTemplate>  
                        <SeparatorTemplate>  
                            <br />  
                        </SeparatorTemplate>  
                    </asp:Repeater>  
                </div>  

This is the code that displays the blog Titles from database table

protected void Page_Load(object sender, EventArgs e)  
    {  
        if (!this.IsPostBack)  
        {  
            this.PopulateArticleId();  
        }  
    }  
    private void PopulateArticleId()  
    {  
        string query = "SELECT [ArticleId], [Title], REPLACE([Title], ' ', '-') [SLUG], [Body] FROM [Articles]";  
        using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security = True"))  
        {  
            using (SqlCommand cmd = new SqlCommand(query))  
            {  
                using (SqlDataAdapter sda = new SqlDataAdapter())  
                {  
                    cmd.Connection = con;  
                    sda.SelectCommand = cmd;  
                    using (DataTable dt = new DataTable())  
                    {  
                        sda.Fill(dt);  
                        rptPages.DataSource = dt;  
                        rptPages.DataBind();  
                    }  
                }  
            }  
        }  
    }  

This is the page where blog contents (i.e. body) will be displayed.
The HTML markup

 <div class="col-sm-10" style="margin: 0 auto; padding: 6px;">  
                   <h5>  
                       <asp:Label ID="lblTitle" runat="server" Text="Title" />  
                       <%--<asp:Label ID="timelbl" runat="server" Text="" Font-Size="Smaller" Font-Bold="true"></asp:Label>--%>  
                   </h5>  
                   <hr />  
                   <asp:Label ID="lblBody" runat="server" Text="Body" />  
               </div>  

This is the code that is supposed to display the contents

    protected void Page_Load(object sender, EventArgs e)  
    {  
        if (!this.IsPostBack)  
        {  
            this.PopulateArticle();  
        }  
    }  
    private void PopulateArticle()  
    {  
        string articleId = this.Page.RouteData.Values["ArticleId"].ToString();  
        string query = "SELECT [Title], [Body] FROM [Articles] WHERE [ArticleId] = @ArticleId";  
        using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security = True"))  
        {  
            using (SqlCommand cmd = new SqlCommand(query))  
            {  
                using (SqlDataAdapter sda = new SqlDataAdapter())  
                {  
                    cmd.Parameters.AddWithValue("@ArticleId", articleId);  
                    cmd.Connection = con;  
                    sda.SelectCommand = cmd;  
                    using (DataTable dt = new DataTable())  
                    {  
                        sda.Fill(dt);  
                        lblTitle.Text = dt.Rows[0]["Title"].ToString();  
                        lblBody.Text = dt.Rows[0]["Body"].ToString();  
                        //timelbl.Text = dt.Rows[0]["DatePosted"].ToString();  
                    }  
                }  
            }  
        }  
    }  

I also did routing using the Global.asax file

    void Application_Start(object sender, EventArgs e)   
    {  
        // Code that runs on application startup  
        ScriptManager.ScriptResourceMapping.AddDefinition("jquery",   
    new ScriptResourceDefinition  
{  
Path = "~/scripts/jquery-1.7.2.min.js",  
DebugPath = "~/scripts/jquery-1.7.2.min.js",  
CdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.1.min.js",  
CdnDebugPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.1.js"  
});  
  
         RegisterRoutes(RouteTable.Routes);  
    }  
      
     static void RegisterRoutes(RouteCollection routes)  
    {  
        routes.MapPageRoute("Default", "home", "~/Default.aspx");  
        routes.MapPageRoute("LoginForm", "login", "~/LoginForm.aspx");  
        routes.MapPageRoute("SignUp", "signup", "~/SignUp.aspx");  
        routes.MapPageRoute("generalsupport", "general-support", "~/generalsupport.aspx");  
         routes.MapPageRoute("overview", "dashboard", "~/overview.aspx");  
         routes.MapPageRoute("PasswordRecovery", "RecoverPassword", "~/PasswordRecovery.aspx");  
        routes.MapPageRoute("blogcontents.aspx", "Articles/{ArticleId}/{Slug}.aspx", "blogcontents.aspx");  
    }  
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,252 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
0 comments No comments
{count} votes

Accepted answer
  1. QiYou-MSFT 4,306 Reputation points Microsoft Vendor
    2022-12-30T09:01:06.577+00:00

    Hi @Donald Symmons ,
    First of all, I reply if and only if your program can read the data in the database. The reason for your problem is that the variable value brought by the database cannot be read in the frontend, we can read it in another way.

    <asp:HyperLink ID="HyperLink1" runat="server" Text="Test" ></asp:HyperLink>  
    
     public string str = "Contact";  
                protected void Page_Load(object sender, EventArgs e)  
                {  
                    HyperLink1.NavigateUrl = str + ".aspx";  
                }  
    

    <asp:HyperLink ID="HyperLink1" runat="server" Text="Test" ></asp:HyperLink>

    This allows you to convert the data read from your database into a string and implement the function. Easy and fast.

    Best Regards
    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. P a u l 10,406 Reputation points
    2022-12-29T17:39:38.927+00:00

    This is just a guess but your other routes redirect to the page prefixed with ~/ whereas the route with the issue doesn't:

       routes.MapPageRoute("blogcontents.aspx", "Articles/{ArticleId}/{Slug}.aspx", "blogcontents.aspx");  
    

    Is that intended?

    EDIT:

    Actually the route above ( Articles/{ArticleId}/{Slug}.aspx ) doesn't match the NavigateUrl from the blog titles page ( <%# string.Format("~/contents/{0}/{1}.aspx", Eval("ArticleId"), Eval("Slug")) %> )