Exercises

GET Request

While reading the chapter, you created a basic Hello, World application using ASP.NET called HelloASPDotNET. Open that project up in Visual Studio, and get ready to add some features. Before you start working, create a new branch so you can revisit where you ended the reading at.

Modify your HelloController class to display a form on a GET request that asks the user for both their name and the language they would like to be greeted in. It should look something like this:

Greeting Form Greeting Form

The resulting form submission should return and display the message, “Bonjour Chris”.

Note

The language is presented in a dropdown, more formally known as a select element. For more information about the select element and how it works, read the MDN documentation .

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
   [HttpGet]
        public IActionResult Index()
        {
            string html = "<form method='post'>" +
                "<input type='text' name='name' />" +
                "<select name='language'><option value='english' selected>English</option>" +
                "<option value='spanish'>Spanish</spanish>" +
                "<option value='bosnian'>Bosnian</option>" +
                "<option value='vietnamese'>Vietnamese</option>" +
                "<option value='french'>French</option></select>" +
                "<input type='submit' value='Greet Me!'/>" +
                "</form>";

            return Content(html, "text/html");

        }

POST Request

When the user submits the form (via a POST request), they should be greeted in the selected language. Your new feature should:

  1. Include at least 5 languages, with English being the default. If you don’t speak 5 languages yourself, ask your friend the Internet .

  2. Include a new public static method, CreateMessage, in the HelloController that takes a name as well as a language string. Based on the language string, you’ll display the proper greeting.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
       public static string CreateMessage(string name, string language)
         {
             string helloTranslation = "";
             switch (language)
             {
                 case "french":
                     helloTranslation = "Bonjour ";
                     break;
                 case "spanish":
                     helloTranslation = "Hola ";
                     break;
                 case "bosnian":
                     helloTranslation = "Zdravo ";
                     break;
                 case "vietnamese":
                     helloTranslation = "Xin Chao ";
                     break;
                 case "english":
                     helloTranslation = "Hello ";
                     break;
             }
             return helloTranslation + name;
    
         }

Bonus Mission

  1. Add some more HTML and inline styles to your returned greeting response string so that the displayed message looks a bit nicer.