ASP.NET MVC Interview Questions with Answers Part2

ASP.NET MVC is a Framework based on MVC pattern and developed by Microsoft Corporation. This framework is very strong, user friendly and has great features to develop and maintains large scale applications. This article explains some important questions and answers in ASP.NET MVC. Hope it will help you to build successful carrier.

What are the different types of action results in MVC?
In MVC there are 12 results. Among them ActionResult class is a base class for all action results. The 11 sub classes are given bellow:

  • ViewResult – renders a view as a web page to the response stream
  • PartialViewResult – renders a view inside another view
  • EmptyResult – returns a empty or null result
  • RedirectResult – redirect to another action method (HTTP redirection)
  • RedirectToRouteResult – redirect to another action method (HTTP redirection) that is determined by the routing engine, based on a given route
  • JsonResult – returns a JSON object form a ViewData
  • JavaScriptResult – returns a script code that can be executed on the client
  • ContentResult – returns a user-defined content type
  • FileContentResult – returns a file to the client
  • FileStreamResult – returns a file to the client, which is supplied by a Stream
  • FilePathResult – returns a file to the client

How to implement Windows authentication in MVC?
For Windows authentication, at first we need to change the web.config file and set the authentication mode to Windows. A sample code for that is given bellow:

 <authentication mode="Windows"/>
  <authorization>
    <deny users="?"/>
  </authorization>

Then in the controller or on the action, we can use the Authorize attribute which specifies which users have access to these controllers and actions. A sample code for that is given bellow, where only the user “Administrator” can access it.
[Authorize(Users= @”WIN-3LI600MWLQN\Administrator”)]

public class StartController : Controller
{
    //
    // GET: /Start/
    [Authorize(Users = @"WIN-3LI600MWLQN\Administrator")]
    public ActionResult Index()
    {
        return View("MyView");
    }
}

How to implement Forms authentication in MVC?
For Windows authentication, at first we need to change the web.config file and set the authentication mode to Forms. Here the login URL points to a controller rather than a page. A sample code for that is given bellow:

  <authentication mode="Forms">
    <forms loginUrl="~/Home/SignIn"  timeout="2660"/>
  </authentication>

Also we need to create a controller where we will check if the user is valid or not. If the user is valid we will set the cookie value. A sample C# code for that is given bellow:

public ActionResult SignIn()
{
    if ((Request.Form["txtUserName"] == "MyUserName") &&
            (Request.Form["txtPassword"] == "MyPassword"))
    {
        FormsAuthentication.SetAuthCookie("MyUserName ", true);
        return View("About");
    }
    else
    {
        return View("Index");
    }
}

We need to use Authorize attribute so that any unauthorized user can’t call to these controllers. Sample C# code is given bellow:

[Authorize]
        Public ActionResult Default()
        {
            return View();
        }

        [Authorize]
        public ActionResult About()
        {
            return View();
        }

What do you know about bundling and minification in MVC?
In MVC, bundling and minification helps us improve request load times of a page. It increases the performance of the applications.

What is bundling and how to implement bundling in MVC?
In a project we need to use CSS and script files. Bundling helps us to combine multiple JavaScript and CSS files into a single file. Thus it minimizes the multiple requests in to a single request.
In order to implement bundling open the BundleConfig.cs file from the App_Start folder. Write the following codes which will combine all the JS files of Scripts folder into a single unit.

What is minification and how to implement minification in MVC?
Minification is a process to remove the blank space, comments etc.
The implementation of minification is as like as bundling. That means when we implement bundling minification is also implemented automatically. A sample JavaScript codes with comments:
// This is test comments
var x = 5;
x = x + 2;
x = x * 3;

After implementing minification the JavaScript code looks like:
var x=5;x=x+12x=x*3

What is an area in MVC?
In MVC, areas help us to organize all the functionalities into independent modules. For a big project which has 100’s of controller classes, it is really difficult to manage them. Areas help us to groups all the classes based on their functionality.

How to implement are in MVC
We can add area by right clicking on the solution and clicking on “Area”. Give an appropriate name for the area. On the solution explorer we will find Controllers, Models, and Views.

How can you handle multiple Submit buttons in MVC?
Consider we have two submit buttons.
We can handle this problem in two ways. First one is normal HTML way and second one is Ajax way.
For HTML way, we need to create two forms and place the submit button inside each of the forms. Sample code is given bellow:

<form action="Action1" method=post>
    <input type="submit" name="Submit1"/>
</form> 
<form action="Action2" method=post>
    <input type="submit" name="Submit2">
</form>

In Ajax way, we can create two different functions (“Fun1” and “Fun1”). We need to bundle each of two functions on button click event. Sample code is given bellow:

<Script language="javascript">
    function Fun1()
    {
        $.post("/Action1",null,CallBack1);
    }
    function Fun2()
    {
        $.post("/Action2",null,CallBack2);
    }
</Script>
<form action="/Action1" method=post>
    <input type=submit name=sub1 onclick="Fun1()"/>
</form>
<form action="/Action2" method=post>
    <input type=submit name=sub2 onclick="Fun2()"/>
</form>

What is Scaffolding in MVC?
Scaffolding is a technique in which MVC template helps us to generate codes for CRUD operations. During controller creations we need to select
…Controller with read/write actions
…Controller with views, using Entity Framework

In MVC what does scaffolding use internally to connect to database?
Scaffolding uses Entity Framework internally to connect to database.

What is a model binder in MVC?
In MVC, model binder acts like a bridge between HTML UI and MVC model. Sometimes the name of HTML UI is different than the model property names. For that case we can write the mapping logic in the binder between the UI and the model.

How can we use multiple models in a single view?
When we bind a model with a view we can select only one model. But if we need to use multiple models classes in single views, we need to create a new model class which aggregates all the models.

Why we need display mode in MVC?
Display Modes is newly feature added in ASP.NET MVC 4. It helps us to change the view automatically depending on the user devices (desktop, smart phone, etc).

Is MVC 4 support Windows Azure SDK?
Yes, it supports.

Which namespace is required for MVC?
System.Web.Mvc assembly

In MVC (Model, View & Controller) which one is best to create first?
It is best to create Model.

Can we create automatically generate the codes for CRUD operations?
Yes, if we define Model first we can generate actions automatically.

What are the helper classes?
Instead of writing Input tag you take help of Helper classes. For your understanding these are alike ASP.NET Server controls. However these have there are lot differences between ASP.NET webforms server controls like Textbox, Dropdown etc..

Can we access Model in cshtml page?
Yes, we can access Models in cshtml Page or views.

Can we share a view across multiple controllers?
Yes, if we put a view into the shared folder.

How to avoid XSS vulnerabilities in MVC?
To avoid XSS vulnerabilities we need to use the syntax ‘<%: %>’ instead of syntax ‘<%= %>’. It is applicable in .Net Framework 4.0. It does the HTML encoding. A sample code is given bellow:

<input type="text" value= "<%: value %>"/>

For more..

Comments

  1. By Abrar

    Reply

  2. By Mira

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *