Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Explain how to create an ASP.NET MVC project with source code.

Answer»

The following steps will explain how to create an ASP.NET MVC-based web application using Visual Studio 2019 IDE.

Creating a Web Project: Open the Visual Studio and click on the menu bar, then select the new option for creating a new project. You can see how to create a new project in the below-given image:

Select Project Type: Select the project type as an ASP.NET Web Application(.NET framework) and click on the Next button.

Provide Project Name: The name of the project should be provided in the following window and then click on Create button.

Select MVC Template: Now, select the MVC web template from the list of templates available, as we are implementing the MVC project. Then click on Create.

MVC Web Application Project Structure: The following image represents the project structure of the project created by us.

You can see that the Project structure is having three folders namely Model, View, and Controller. The default controller for the created application is the HomeController. By default, the HomeController consists of the following code:

// HomeController.vbPublic CLASS HomeController Inherits System.Web.Mvc.Controller Function Index() As ActionResult Return View() End Function Function About() As ActionResult ViewData("Message") = "Your application description page." Return View() End Function Function Contact() As ActionResult ViewData("Message") = "Your contact page." Return View() End FunctionEnd Class

The Index file in the views folder will be the default file for the home controller.

// index.vbhtml@Code ViewData("Title") = "Home Page"End Code<div class="jumbotron"> <H1>ASP.NET</h1> <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p> <p><a href="https://asp.net" class="btn btn-primary btn-lg">Learn more »</a></p></div><div class="row"> <div class="col-md-4"> <h2>Getting started</h2> <p> ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that enables a clean separation of concerns and gives you full control over markup for enjoyable, agile development. </p> <p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301865">Learn more »</a></p> </div> <div class="col-md-4"> <h2>Get more libraries</h2> <p>NuGet is a free Visual Studio extension that MAKES it easy to add, remove, and update libraries and tools in Visual Studio projects.</p> <p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301866">Learn more »</a></p> </div> <div class="col-md-4"> <h2>Web Hosting</h2> <p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p> <p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301867">Learn more »</a></p> </div></div>

The above default code in the project will produce the below-given output when it is viewed in the BROWSER:

Conclusion

The MVC pattern of ASP.NET has various advantages because of the separation between business logic, input logic, and user interface(UI) logic. But it has a few drawbacks such as it is completely controlled by JavaScript, HTML, and CSS, also it cannot be easily learned without any experiments.

To become industry competent, apart from learning the ASP.NET MVC, one must learn the commonly used C# programming language, SQL Server, OOPs, and some of the front-end technologies such as JavaScript, HTML, JQuery, etc.

Useful Interview Resources:

  • .NET Interview Questions
  • OOPs Interview Questions
  • C# Interview Questions
  • SQL Interview Questions
2.

How do you manage unhandled exceptions in the Action method?

Answer»

The HandleError attribute is used to manage the UNHANDLED exceptions in the action method of the ASP.NET MVC application. CONSIDER that the HandleError is not used in the application, and in CASE an unhandled EXCEPTION is thrown by an action method then the default error page with sensitive information will be displayed to everybody.

It requires enabling custom errors in the web.config FILE, before the usage of HandleError.

<system.web> <customErrors mode="On" defaultRedirect="Error" /></system.web>

Few properties of HandleError can be used for handling the exception. They are:

  • ExceptionType
  • Master
  • View
  • Order.
3.

Can we overload the action methods?

Answer»

It is possible to overload the action methods by passing the NAME of the method in the ActionName ATTRIBUTE as given below:

[ActionName("ActionMethodExample")]public ActionResult getInfo(INT id){ return "";}

Another action method with the same name can be declared as given below:

public ActionResult getInfo(){ return "";}

Now, these two getInfo() methods can be overloaded as they are having the same FUNCTION name but DIFFER in the number of arguments.

4.

Consider you want to restrict the users from requesting the method or Action directly in the URL of the browser. How it can be done?

Answer»

TWO of the methods to ACHIEVE the result for this scenario are:

Option 1:

The ChildActionMethod will permit you for restricting access to ACTION methods that you don’t want users to directly REQUEST in the URL of the browser.

[ChildActionOnly] public ActionResult demoPiper() { return View(); }

Now, the demoPiper() action method cannot be directly requested by the user through the URL of the browser.

Option 2:

In ASP.NET MVC, each controller’s method is accessed through a URL, and in case you don’t want an action CREATED by you to be accessible through URL then you need to protect your action method with the help of the NonAction attribute provided by the MVC.

[NonAction]public ActionResult demoPiper() { return "demoPiper.com"; }

After the demoPiper() method is declared as NonAction, if you try to access the method or action through URL, then HTTP 404 Not Found error will be returned.

5.

Consider that you are developing an application that uses forms authentication in ASP.NET MVC.

Answer»

A user named AdminLibrary is present in the user database. You will have the requirements as FOLLOWS: - All users must be allowed to access the obtainBook() method. - Access to the modifyBook() method must be restricted for the AdminLibrary user. The controller must be implemented to meet the requirements. Which CODE segment will be USED by you?

[Authorize]public class LibraryControllerDemo:Controller{ [Allowanonymous] public ACTIONRESULT obtainBook() { return VIEW(); } [Authorize("AdminLibrary")] public actionresult modifyBook() { return view(); }}

[Allowanonymous] - It will allow all users to access the obtainBook() method.
[Authorize("AdminLibrary")]- It will restrict the AdminLibrary user from accessing the modifyBook() method.