tike
tike

Reputation: 548

creating dynamic html c#

i wanted to create a web server while on the process.....

i am not being able to create a dynamic html which could take link from my c# console application...

for example i have a code which shows files from the system.. for example "c:\tike\a.jpeg" now i wanted to make that particular link a a href link in my html page...

any help would be appreciated..... thank you..

(to sum up ... i want to create dynamic html page which takes value from c# console application.)

Upvotes: 2

Views: 10074

Answers (1)

dtb
dtb

Reputation: 217263

Ignoring virtual paths etc. for now, here is a simple example to get you started:

StringBuilder sb = new StringBuilder();
sb.AppendLine("<html>");
sb.AppendLine("<head>");
sb.AppendLine("<title>Index of c:\\dir</title>");
sb.AppendLine("</head>");
sb.AppendLine("<body>");
sb.AppendLine("<ul>");

string[] filePaths = Directory.GetFiles(@"c:\dir");
for (int i = 0; i < filePaths.Length; ++i) {
    string name = Path.GetFileName(filePaths[i]);

    sb.AppendLine(string.Format("<li><a href=\"{0}\">{1}</a></li>",
        HttpUtility.HtmlEncode(HttpUtility.UrlEncode(name)),
        HttpUtility.HtmlEncode(name)));
}

sb.AppendLine("</ul>");
sb.AppendLine("</body>");
sb.AppendLine("</html>");
string result = sb.ToString();

result contains a string that you can send as body of an HTTP response to the web-browser.

(Note: I typed the code right into the answer box, no idea if it compiles as-is.)

Upvotes: 6

Related Questions