Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1039035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:03:00+00:00 2026-05-16T15:03:00+00:00

I’m finding myself hardcoding html into C#. It’s usually on a data-driven pages. The

  • 0

I’m finding myself hardcoding html into C#. It’s usually on a data-driven pages. The content of the page and url change but the physical page does not change. As it stands now, I have snippets of hardcoded HTML sprinkled throughout the UI. It’s usually navigation html, but not always.

In most cases there is logic wrapped around the html. An exampled would selecting the current tab.

It feels dirty – there must be a more elegant solution.

Maybe using the settings file? Database? The database seems a little overkill and add a couple layers of abstraction that does not have value, at least in my application.

Update

I’ve provided two sample methods. The application is written on the ASP.NET MVC 2 framework.

Example one:

    /// <summary>
    /// Gets the album sub navigation.
    /// </summary>
    /// <returns></returns>
    public string GetAlbumSubNavigation()
    {
        StringBuilder builder = new StringBuilder();
        IDictionary<string, string> links = new Dictionary<string, string>
                                                {
                                                    {"all",  @"<li><a {2} href=""/{0}/albums/addphotos/{1}"">All</a></li>"},
                                                    {"bytags",  @"<li><a {2} href=""/{0}/albums/tags/{1}"">Tags</a></li>"},
                                                    {"bysearching", @"<li><a {2} href=""/{0}/albums/searchphotos/{1}"">Search</a></li>"}
                                                };

        builder.AppendLine(@"<ul style=""text-align: right; margin-bottom: 40px;"" class=""hyperlinks"" id=""subviewlinks"">");
        foreach (KeyValuePair<string, string> keyValuePair in links)
        {
            string youarehere = (string.Equals(PartialViewName, keyValuePair.Key,
                                               StringComparison.InvariantCultureIgnoreCase)
                                     ? "class=\"youarehere\""
                                     : string.Empty);
            builder.AppendLine(string.Format(keyValuePair.Value, Authorization.Owner.Username, Album.AlbumId,
                                             youarehere));
        }
        builder.AppendLine(@"</ul>");
        return builder.ToString();
    }

Example two:

    /// <summary>
    /// Gets the photo detail links.
    /// </summary>
    /// <param name="media">The media.</param>
    /// <param name="isAuthenticated">if set to <c>true</c> [is authenticated].</param>
    /// <returns></returns>
    public static string GetPhotoDetailLinks(Media media, bool isAuthenticated)
    {
        IUserUrlService userUrlService = GetUserUrlService();
        ITagService tagService = DependencyInjection.Resolve<ITagService>();

        const string perminateLinkFormat = @"<li><a href=""{0}"" title=""{1}"" >permalink</a></li>";
        string perminateLink = string.Format(perminateLinkFormat, userUrlService.UserUrl(media.Owner.Username, "photos/show/" + media.MediaId), media.Title);

        string html = @"<ul>
                    <li>
                    <span>";
        html += (isAuthenticated ? @"<a id=""editlink""  href=""{0}/photos/edit/{1}"">edit</a>" : string.Empty);
        html += @"</span>            
                </li>";
        html += "{2}";
        html += @"          
                </li>
                 {3}  
                 {5}                  
                <li><span><a href=""{0}/comments/leave/{1}"">comments ({4})</a></span> 
            </ul>";

        string tags = string.Empty;
        if (!string.IsNullOrEmpty(media.Tags))
        {
            const string tagFormat = @"<li><span>tags:</span> {0}</li>";
            string renderedTags = tagService.HyperlinkTheTags(media.Tags, media.Owner.Username);
            tags = string.Format(tagFormat, renderedTags);
        }

        string date = GetDate(media);

        string content = string.Format(html, userUrlService.UserRoot(media.Owner.Username), HttpUtility.HtmlEncode(media.MediaId.ToString()), date, tags, media.CommentCount, perminateLink);
        return content;
    }
  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:03:00+00:00Added an answer on May 16, 2026 at 3:03 pm

    Take a look at specifying your HTML as a template and substituting content at runtime through template variables. See this post for some ideas using T4 or XSLT: C# template engine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.