Synechron Frequently Asked ASP.NET MVC Interview Questions

Synechron Most Frequently Asked ASP.NET MVC Interview Questions Answers

Explain the methods used to render the views in MVC?

Below are the methods used to render the views from action -

View() — To return the view from action.

PartialView() — To return the partial view from action.

RedirectToAction() — To Redirect to different action which can be in same controller or in different controller.

Redirect() — Similar to “Response.Redirect()” in webforms, used to redirect to specified URL.

RedirectToRoute() — Redirect to action from the specified URL but URL in the route table has been matched.

What are the sub types of ActionResult?

ActionResult is used to represent the action method result. Below are the subtypes of ActionResult –

ViewResult

PartialViewResult

RedirectToRouteResult

RedirectResult

JavascriptResult

JSONResult

FileResult

HTTPStatusCodeResult
Synechron Most Frequently Asked ASP.NET MVC Interview Questions Answers
Synechron Most Frequently Asked ASP.NET MVC Interview Questions Answers

What are Non Action methods in MVC?

In MVC all public methods have been treated as Actions. So if you are creating a method and if you do not want to use it as an action method then the method has to be decorated with “NonAction” attribute as shown below –

[NonAction]

public void TestMethod()

{

// Method logic

}

How to change the action name in MVC?

“ActionName” attribute can be used for changing the action name. Below is the sample code snippet to demonstrate more –

[ActionName(“TestActionNew”)]

public ActionResult TestAction()

{

return View();

}

So in the above code snippet “TestAction” is the original action name and in “ActionName” attribute, name — “TestActionNew” is given. So the caller of this action method will use the name “TestActionNew” to call this action.

What is Html.RenderPartial?

Result of the method — “RenderPartial” is directly written to the HTML response. This method does not return anything (void). This method also does not depend on action methods. RenderPartial() method calls “Write()” internally and we have to make sure that “RenderPartial” method is enclosed in the bracket. Below is the sample code snippet –@{Html.RenderPartial(“TestPartialView”); }

What are the components required to create a route in MVC?

Name — This is the name of the route.

URL Pattern — Placeholders will be given to match the request URL pattern.

Defaults –When loading the application which controller, action to be loaded along with the parameter.

Why to use “{resource}.axd/{*pathInfo}” in routing in MVC?

Using this default route — {resource}.axd/{*pathInfo}, we can prevent the requests for the web resources files like — WebResource.axd or ScriptResource.axd from passing to a controller.

Can we add constraints to the route? If yes, explain how we can do it?

Yes we can add constraints to route in following ways –

Using Regular Expressions

Using object which implements interface — IRouteConstraint.

What are the possible Razor view extensions?

Below are the two types of extensions razor view can have –

.cshtml — In C# programming language this extension will be used.

.vbhtml — In VB programming language this extension will be used.

What is PartialView in MVC?

PartialView is similar to UserControls in traditional web forms. For re-usability purpose partial views are used. Since it’s been shared with multiple views these are kept in shared folder. Partial Views can be rendered in following ways –

Html.Partial()

Html.RenderPartial().

Mention what are the file extensions for razor views?

For razor views the file extensions are

.cshtml: If C# is the programming language
.vbhtml: If VB is the programming language

Mention what are the two ways for adding constraints to a route?

Two methods for adding constraints to route is

Using regular expressions
Using an object that implements IRouteConstraint interface

Mention how can maintain session in MVC?

Session can be maintained in MVC by three ways tempdata, viewdata, and viewbag.

Mention what is the difference between Temp data, View, and View Bag?

Temp data: It helps to maintain data when you shift from one controller to other controller.
View data: It helps to maintain data when you move from controller to view
View Bag: It’s a dynamic wrapper around view data

What is partial view in MVC?

Partial view in MVC renders a portion of view content. It is helpful in reducing code duplication. In simple terms, partial view allows to render a view within the parent view.

Explain how you can implement Ajax in MVC?

In Ajax, MVC can be implemented in two ways

Ajax libraries
Jquery.

What is ASP.NET MVC?

ASP.NET MVC is a web application Framework. It is light weight and highly testable Framework. MVC separates application into three components — Model, View and Controller.

What is Razor View Engine?

Razor is the first major update to render HTML in MVC 3. Razor was designed specifically for view engine syntax. Main focus of this would be to simplify and code-focused templating for HTML generation. Below is the sample of using Razor:

@model MvcMusicStore.Models.Customer
@{ViewBag.Title = “Get Customers”;}
<div class=”cust”> <h3><em>@Model.CustomerName</em> </h3>

What you mean by Routing in MVC?

Routing is a pattern matching mechanism of incoming requests to the URL patterns which are registered in route table. Class — “UrlRoutingModule” is used for the same process.

What are Actions in MVC?

Actions are the methods in Controller class which is responsible for returning the view or json data. Action will mainly have return type — “ActionResult” and it will be invoked from method — “InvokeAction()” called by controller.

What is Attribute Routing in MVC?

ASP.NET Web API supports this type routing. This is introduced in MVC5. In this type of routing, attributes are being used to define the routes. This type of routing gives more control over classic URI Routing. Attribute Routing can be defined at controller level or at Action level like –

[Route(“{action = TestCategoryList}”)] — Controller Level
[Route(“customers/{TestCategoryId:int:min(10)}”)] — Action Level

Post a Comment

Previous Post Next Post