February 8, 2018

Sreekanth B

TCS ASP.NET MVC Recent Interview Questions Answers


TCS ASP.NET MVC Recent Most common Interview Questions And Answers 2018

What is GET and POST Actions Types?

GET

GET is used to request data from a specified resource. With all the GET request we pass the URL which is compulsory, however it can take the following overloads.

.get(url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] ).done/.fail

POST

POST is used to submit data to be processed to a specified resource. With all the POST requests we pass the URL which is compulsory and the data, however it can take the following overloads.

.post(url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] )

What is display mode in MVC?

Display mode displays views depending on the device the user has logged in with. So we can create different views for different devices and display mode will handle the rest.
TCS ASP.NET MVC Recent Interview Questions Answers
For example we can create a view “Home.aspx” which will render for the desktop computers and Home.Mobile.aspx for mobile devices. Now when an end user sends a request to the MVC application, display mode checks the “user agent” headers and renders the appropriate view to the device accordingly.

How can we do exception handling in MVC?

In the controller you can override the “OnException” event and set the “Result” to the view name which you want to invoke when error occurs. In the below code you can see we have set the “Result” to a view named as “Error”.

We have also set the exception so that it can be displayed inside the view.

public class HomeController : Controller

 {

        protected override void OnException(ExceptionContext filterContext)

        {

            Exception ex = filterContext.Exception;

            filterContext.ExceptionHandled = true;

     var model = new HandleErrorInfo(filterContext.Exception, "Controller","Action");

     filterContext.Result = new ViewResult()

{

                ViewName = "Error",

                ViewData = new ViewDataDictionary(model)

     };

        }
}

To display the above error in view we can use the below code

@Model.Exception;

What is the use of remote validation in MVC?

Remote validation is the process where we validate specific data posting data to a server without posting the entire form data to the server. Let's see an actual scenario, in one of my projects I had a requirement to validate an email address, whether it already exists in the database. Remote validation was useful for that; without posting all the data we can validate only the email address supplied by the user.

Let's create a MVC project and name it accordingly, for me its “TestingRemoteValidation”. Once the project is created let's create a model named UserModel that will look like:

public class UserModel

{

    [Required]

    public string UserName

    {

        get;

        set;

    }

    [Remote("CheckExistingEmail", "Home", ErrorMessage = "Email already exists!")]

    public string UserEmailAddress

    {

        get;

        set;

    }

}

Let's get some understanding of the remote attribute used, so the very first parameter “CheckExistingEmail” is the the name of the action. The second parameter “Home” is referred to as controller so to validate the input for the UserEmailAddress the “CheckExistingEmail” action of the “Home” controller is called and the third parameter is the error message. Let's implement the “CheckExistingEmail” action result in our home controller.

public ActionResult CheckExistingEmail(string UserEmailAddress)

{

    bool ifEmailExist = false;

    try

    {

        ifEmailExist = UserEmailAddress.Equals("mukeshknayak@gmail.com") ? true : false;

        return Json(!ifEmailExist, JsonRequestBehavior.AllowGet);

    } catch (Exception ex)

    {

        return Json(false, JsonRequestBehavior.AllowGet);

    }

}

Explain Dependency Resolution?

Dependency Resolver again has been introduced in MVC3 and it is greatly simplified the use of dependency injection in your applications. This turn to be easier and useful for decoupling the application components and making them easier to test and more configurable.

Explain Bundle.Config in MVC4?

"BundleConfig.cs" in MVC4 is used to register the bundles by the bundling and minification system. Many bundles are added by default including jQuery libraries like - jquery.validate, Modernizr, and default CSS references.

What is the meaning of Unobtrusive JavaScript?

This is a general term that conveys a general philosophy, similar to the term REST (Representational State Transfer). Unobtrusive JavaScript doesn't intermix JavaScript code in your page markup.

Eg : Instead of using events like onclick and onsubmit, the unobtrusive JavaScript attaches to elements by their ID or class based on the HTML5 data- attributes.

What is ViewStart?

Answer:

Razor View Engine introduced a new layout named _ViewStart which is applied on all view automatically. Razor View Engine firstly executes the _ViewStart and then start rendering the other view and merges them.

Example of Viewstart:
@ {
    Layout = "~/Views/Shared/_v1.cshtml";
} < !DOCTYPE html >
    < html >
    < head >
    < meta name = "viewport"
content = "width=device-width" / >
    < title > ViewStart < /title> < /head> < body >
    < /body> < /html>.


Subscribe to get more Posts :