MVC Interview Question - 2

How can I return string result from Action in ASP.Net MVC?

Below is the code snippet to return string from action method :

public ActionResult TestAction() {

return Content("Hello Test !!");


How to return the JSON from action method in ASP.Net MVC?

Below is the code snippet to return string from action method :

public ActionResult TestAction() {

return JSON(new { prop1 = "Test1", prop2 = "Test2" });


What is the Razor in ASP.Net

Razor is not a new programming language itself, but uses C# syntax for embedding code in a page without 

the ASP.NET delimiters: <%= %> . 

It is a simple-syntax view engine and was released as part of ASP.NET MVC 3. 

The Razor file extension is "cshtml" for the C# language

Read below article for more about Razor.

Explain what is routing? What are the three segments for routing is important?

Routing helps you to decide a URL structure and map the URL with the Controller.

The three segments that are important for routing is



Explain how routing is done in MVC pattern?

There is a group of routes called the RouteCollection, which consists of registered routes in the application.  The RegisterRoutes method records the routes in this collection.  A route defines a URL pattern and a handler to use if the request matches the pattern. The first parameter to the MapRoute method is the name of the route. The second parameter will be the pattern to which the URL matches.  The third parameter might be the default values for the placeholders if they are not determined.

Where is the route mapping code written in MVC?

The route mapping code is written in "RouteConfig.cs" file and registered using "global.asax" application start event.

For more details

Can we map multiple URL’s to the same action?

Yes, you can, you just need to make two entries with different key names and specify the same controller and action.

What is the attribute based routing in MVC 

This new feature introduce in MVC 5.

By using the "Route" attribute we can define the URL structure. 
For example in the below code we have decorated the "GoAboutus" action with the route attribute. 
The route attribute says that the "GoAboutus" can be invoked using the URL structure "Users/about".

public class HomeController : Controller



       public ActionResult GoAboutus()


           return View();



How to enable Attribute Routing?

Just add @Model.CustomerName the method : "MapASP.Net MVCAttributeRoutes()" to enable attribute routing as shown below:

public static void RegisterRoutes(RouteCollection routes)
        //enabling attribute routing
        routes.MapASP.Net MVCAttributeRoutes();
        //convention-based routing
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Customer", action = "GetCustomerList", id = UrlParameter.Optional }

Mention what are the two ways to add constraints to a route?

The two methods to add constraints to a route is

Use regular expressions
Use an object that implements IRouteConstraint Interface

Below is example of Regular expression

    new {controller="Product", action="Details"},
    new {productId = @"\d+" }

The regular expression \d+ matches one or more integers. This constraint causes the Product route to match the following URLs:


But not the following URLs:



What is the use of the default route {resource}.axd/{*pathinfo} ?

This default route prevents request for a web resource file such as Webresource.axd or ScriptResource.axd from being passed to the controller.

What are child actions in ASP.Net MVC?

To create reusable widgets child actions are used and this will be embedded into the parent views. In ASP.Net MVC Partial views are used to have reusability in the application. Child action mainly returns the partial views

What is the ChildActionOnly attribute ?

The ChildActionOnly attribute ensures that an action method can be called only as a child method from within a view. An action method doesn’t need to have this attribute to be used as a child action, but we tend to use this attribute to prevent the action methods from being invoked as a result of a user request. Having defined an action method, we need to create what will be rendered when the action is invoked. Child actions are typically associated with partial views, although this is not compulsory.

1. [ChildActionOnly] allowing restricted access via code in View
2. State Information implementation for specific page URL. Example: Payment Page URL (paying only once) razor syntax allows to call specific actions conditional


public ActionResult MenuBar()


//Logic here

return PartialView();


MVC Interview Question                           MVC Interview Question - 3

Posted in Labels: , |


Post a Comment