1.

Write a program to calculate the addition of two numbers.

Answer»

The steps are as follows:

1. You need to create a new ASP.NET Core Project “CalculateSum”. Open Visual Studio 2015, GOTO File–> New–> Project. SELECT the option Web in Left Pane and go for the option ASP.NET Core Web Application (.NET Core) under the central pane. Edit the project name as “CalculateSum” and click on OK.

2. In the TEMPLATE window, select Web Application and set the Authentication into “No Authentication” and click on OK.

3. Open “Solution Explorer” and right-click on the folder “Home” (It is Under Views), then click on Add New Item. You need to select MVC View Page Template under ASP.NET Section and rename it as “addition.cshtml” and then click on the Add button.

4. Open addition.cshtml and write the following code:

 @{ ViewBag.Title = "Addition Page";}<h1>Welcome to Addition Page</h1><form asp-controller="Home" asp-action="add" method="post"> <span>Enter First Number : </span> <input id="TEXT1" type="text" name="txtFirstNum" /> <br /><br /> <span>Enter Second Number : </span> <input id="Text2" type="text" name="txtSecondNum" /> <br /><br /> <input id="Submit1" type="submit" value="Add" /></form><h2>@ViewBag.Result</h2>

Here, we have created a simple form that is having two text boxes and a single Add Button. The text boxes are named as txtFirstNum and txtSecondNum. On the controller page, we can access these textboxes using:
<form asp-controller="Home" asp-action="add" method="post">

This form will indicate all the submissions will be moved to HomeController and the method add action will be executed.

5. Open the HomeController.cs and write the following code onto it:

 using System;using Microsoft.AspNetCore.Mvc;namespace CalculateSum.Controllers{ public class HomeController : Controller { public IActionResult Index() { return View(); } public IActionResult About() { ViewData["Message"] = "Application description page."; return View(); } public IActionResult Contact() { ViewData["Message"] = "Contact page."; return View(); } public IActionResult Error() { return View(); } public IActionResult addition() { return View(); } [HttpPost] public IActionResult add() { int number1 = Convert.ToInt32(HttpContext.Request.Form["txtFirstNum"].ToString()); int number2 = Convert.ToInt32(HttpContext.Request.Form["txtSecondNum"].ToString()); int res = number1 + number2; ViewBag.Result = res.ToString(); return View("addition"); } }}

In this program, we have added two IAction Methods addition() and add(). Addition() method will return the addition view page and add() method obtains input from the browser, processes it, and results will be kept in ViewBag.Result and then returned to the browser.

Now, press Ctrl+F5 for running your program. This will launch an ASP.NET Core WEBSITE into the browser. Add /Home/addition at the end of the link and then hit on enter. The output format is given below:

Conclusion

The .NET is a full-stack software development framework, which is essentially used to build large enterprise-scale and scalable software applications. The .NET framework has wide scope in the market. It is a flexible and user-friendly framework, that goes well along with other technologies.

The .NET Core was developed in response to the surge in Java popularity. The .NET Core is normally used in low-risk projects. Some of the .NET components can be used in .NET core applications (but not the other way around). This article mainly concentrates on the framework concepts of .Net and .NET Core. We are sure that it would give you sufficient information and a fair knowledge of the common questions that will be asked during an interview.

Useful Resources:

.Net Developer: Career Guide

ASP. NET Interview

C# Interview



Discussion

No Comment Found