Untitled

 avatar
JCallico
plain_text
2 months ago
4.5 kB
2
Indexable
Never
Here is the equivalent C# code for the provided Perl `return_html` subroutine. Note that some parts of the code, like the HTML output, have been adapted for C# syntax. Adjustments may be needed based on your specific requirements.

```csharp
using System;
using System.Collections.Specialized;

class Program
{
    static void Main()
    {
        // Example usage
        NameValueCollection config = new NameValueCollection
        {
            { "recipient", "recipient@example.com" },
            { "title", "Form Submission Confirmation" },
            // Add other configuration values as needed
        };

        NameValueCollection form = new NameValueCollection
        {
            { "realname", "John Doe" },
            { "email", "john.doe@example.com" },
            // Add other form fields as needed
        };

        ReturnHtml(config, form);
    }

    static void ReturnHtml(NameValueCollection config, NameValueCollection form)
    {
        // If redirect option is used, print the redirection location header.
        if (!string.IsNullOrEmpty(config["redirect"]))
        {
            Console.WriteLine($"Location: {config["redirect"]}\n\n");
        }
        // Otherwise, begin printing the response page.
        else
        {
            // Print HTTP header and opening HTML tags.
            Console.WriteLine("Content-type: text/html\n");
            Console.WriteLine("<html>\n <head>\n");

            // Print out title of page
            Console.WriteLine(config["title"] != null ? $"  <title>{config["title"]}</title>\n" : "  <title>Thank You</title>\n");

            Console.WriteLine(" </head>\n <body");

            // Get Body Tag Attributes
            // Implement body_attributes function logic here

            Console.WriteLine(">\n  <center>\n");

            // Print custom or generic title.
            Console.WriteLine(config["title"] != null ? $"   <h1>{config["title"]}</h1>\n" : "   <h1>Thank You For Filling Out This Form</h1>\n");

            Console.WriteLine("</center>\n");

            Console.WriteLine($"Below is what you submitted to {config["recipient"]} ");
            Console.WriteLine("<br>on " + DateTime.Now + "<p><hr size=1 width=75%><p>\n");

            //BSS Table output for HTML
            // Also realname and email fields
            Console.WriteLine("<table cellspacing=2 cellpadding=1>");
            if (!string.IsNullOrEmpty(form["realname"]))
            {
                Console.WriteLine($"<tr><td align=right><b>Name:</b></td><td align=left>{form["realname"]}</td></tr>\n");
            }

            if (!string.IsNullOrEmpty(form["email"]))
            {
                Console.WriteLine($"<tr><td align=right><b>E-mail:</b></td><td align=left>{form["email"]}</td></tr>\n\n");
            }
            //BSS

            // Sort alphabetically if specified:
            foreach (string field in form.Keys)
            {
                // If the field has a value or the print blank fields option is turned on, print out the form field and value.
                if (config["print_blank_fields"] != null || !string.IsNullOrEmpty(form[field]))
                {
                    //BSS - table output
                    Console.WriteLine($"<tr><td align=right>{field}:</td><td align=left>{form[field]}</td></tr>\n");
                    //BSS
                }
            }

            //BSS
            Console.WriteLine("</table><br clear=all>\n");
            //BSS

            Console.WriteLine("<p><hr size=1 width=75%><p>\n");

            // Check for a Return Link and print one if found.
            if (!string.IsNullOrEmpty(config["return_link_url"]) && !string.IsNullOrEmpty(config["return_link_title"]))
            {
                Console.WriteLine("<ul>\n");
                Console.WriteLine($"<li><a href=\"{config["return_link_url"]}\">{config["return_link_title"]}</a>\n");
                Console.WriteLine("</ul>\n");
            }

            // Print the page footer.
            Console.WriteLine("<hr size=1 width=75%><p>\n");
            Console.WriteLine("</body>\n</html>");
        }
    }
}
```

In this C# code, I've used the `Console.WriteLine` statements to simulate the output that would be sent to the response stream in a CGI environment. You may need to adapt this code to fit the specific environment or framework you are using. The HTML output has been modified accordingly to match C# syntax. Adjustments may be needed based on your specific requirements.
Leave a Comment