NET Centric Computing - Old Questions

12. Write short notes on (Any TWO)

        a. Single page application

        b. Hidden fields

        c. Await patterns

5 marks | Asked in Model Question

a) Single Page Application

A single-page application (SPA) is a web application or website that interacts with the user by dynamically rewriting the current web page with new data from the web server, instead of the default method of the browser loading entire new pages. The goal is faster transition that make the website feel more like a native app.

In a single-page application, all necessary HTML, JavaScript, and CSS code is either retrieved by the browser with a single page load, or the appropriate resources are dynamically loaded and added to the page as necessary, usually in response to user actions. The page does not reload at any point in the process, nor does it transfer control to another page, although the location hash or the HTML5 History API can be used to provide the perception and navigability of separate logical pages in the application.


b) Hidden Fields

HiddenField, as name implies, is hidden. Sometimes we require some data to be stored on the client side without displaying it on the page. This is non visual control in Asp.net Core where we can save the value.  This is one of client-side state management tools. It stores the value between the roundtrip. We can save data in hidden form fields and send back in the next request. For example:

[HttpGet]

public IActionResult SetHiddenFieldValue() {

User newUser = new User() {

    Id = 210, Name = "Sushma", Age = 23

};

return View(newUser);

}

[HttpPost]

public IActionResult SetHiddenFieldValue(IFormCollection keyValues) {

    var id = keyValues["Id"];

    return View();

}

In the View, we can create a hidden field and bind the Id value from Model:    @Html.HiddenFor(model =>model.Id)

Then we can use a submit button to submit the form: <input type="submit" value="Submit" />

The general html code for the hidden field look like this when it is inspected: 

    <input type = "hidden" id = "Id" name = "Id" value="210">


c) Await Patterns

The await keyword provides a non-blocking way to start a task, then continue execution when that task completes.

The await keyword is used to asynchronously wait for a Task or Task<T> to complete. It pauses the execution of the current method until the asynchronous task that’s being awaited completes. The difference from calling .Result or .Wait() is that the await keyword sends the current thread back to the thread pool, instead of keeping it in a blocked state. For example:

class Program { static void Main(string[] args) { Method1(); Method2(); Console.ReadKey(); } public static async Task Method1() { await Task.Run(() => { for (int i = 0; i < 100; i++) { Console.WriteLine(" Method 1"); // Do something Task.Delay(100).Wait(); } }); } public static void Method2() { for (int i = 0; i < 25; i++) { Console.WriteLine(" Method 2"); // Do something Task.Delay(100).Wait(); } } }