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;
}