MVC Interview Questions and Answers
MVC Interview Questions and Answers
ASP.Net MVC
Interview Question & Answer’s
1. What is MVC?
MVC Stands for Model View Controller.
1. Model.
2. View.
3. Controller.
ASP.NET MVC Q&A Mr.R.N.Reddy
3. JavaScript.
4. Angular JS.
5. JQuery.
6. C#.
They were
1. MODEL
2. VIEW.
3. CONTROLLER.
No
Yes.
1. Separation of Code.
2. Loosely Coupled (Less Dependent).
3. Parallel Development
4. Easy to perform unit testing.
5. TDD Support
6. Clean & clear URL’s
7. Improved performance.
8. More controlling over HTML.
9. Added new concepts.
10. Easy to learn and Easy to implement.
All URL’s of MVC are controller based and not file based wherein
ASP.Net URL’s are file based.
MVC URL’s are called as clean and clear URL’s. Because it contains less
no of characters (query string) in URL. Due to this feature SEO will be
easy.
/Product.aspx
/Product.aspx?cat=5
/Product.aspx?cat=5&sub=17
/Products
/Products/Shirts
/Products/Shirts/Kids.
ASP.Net WebForms will process the webpage with lot of activities like
below.
1. Page Events.
2. Server control Objects
3. Load request data
4. Process the values
5. Convert to HTML and so on…
Due to above reason we can integrate any kind of client side libraries
with MVC views.
Ex: JQuery, Angular JS, Boot Strap…. are client side libraries.
They are
a) Filters
b) Routing
c) Attribute
d) WEB API
e) Scaffold templates….etc.
While developing the ASP.Net MVC application we can reuse the most
of the C#.Net & ASP.Net Web Forms concepts.
Web API Services follows only HTTP Protocol that’s Web API
services are called as HTTP Services.
They are:
1.App_Start.
2.Content.
3.Controllers.
4.Models.
5.Scripts.
6.Views.
App_Start folder.
Content folder.
Content folder.
Controller folder will contains all controller class files (.cs files).
Yes, we can have multiple controllers but every controller file have
to be a “Csharp class file” ,extension will be .cs file
We can add user defined client side scripting files in scripts folder.
In ASP.Net MVC Views are developed with C# code and HTML tags.
Controller
Object Icontroller
Controller base
Controller
Student Controller
Icontroller.
No
Public
ViewResult.
Predefined
Action method that will return view page as output those methods
come under this type.
These method return statement will be like
below return view()
These methods required a view page with corresponding
method name
Example: Index.cshtml.
Action method with non viewResult that will return data instead of
html formatted output.[webpage].
[“Non Action”].
AJAX.
[NonActionAttribute].
[HTTPGetAttribute].
[HTTPPostAttribute].
[NonActionAttribute].
Microsoft has developed two view Engines that are integrated with
ASP.Net MVC
1. WebForm (.aspx) View Engine.
2. Razor View Engine
String uname=”hari”
<h1>Welcome to @uname<h1>
o/p: Welcome to hari.
@{ C# Statement}
Ex:@{ string name=”haribabu”}
@{
int x=10;
if(x%2==0)
{
<h1>@x is even<h1>
}
else
{
<h1>@x is odd<h1>
}}
Get Request
Post Request
Get Request:
We can make request by entering the url or clicking
the hyperlink.
The purpose of get request is to get the webpage from server.
Post Request
We can make post request by clicking the submit
button. Browser will submit the request based on action
method attribute.
For Ex: <form action=”Home/Index method =”post”</form>
String hari=Request[“textboxname”];
Using (this.IsPost).
WCF Interview Questions
1) What we can do by using WCF?
Ans:A unit of code which is providing services to multiple client applications is called as web
service
Ans: Whenever multiple applications required same functionality, we will develop that common
functionality as a web service and which can be consumed by required applications.
Ans: If service provider and service receiver developed by using same technology which are
called as homogeneous applications.
Ans: If the service provider developed by one technology and service receiver developed by
another technology which are called as heterogeneous applications.
8) What is interoperability?
Ans: Whenever service provider and service receiver are in different platform, it means one
technology web service is consumed by another technology developed application. For eg: Dotnet
web service is consumed by Java client application as well as Java web service is consumed by
Dotnet client application.
Ans:Due to limitations of xml web services, Microsoft has introduced a new web service
technology i.e. WCF with .Net 3.0 version in the year 2006.
Ans:
[ServiceContract]
public interface IService1
{
//here we have to declare WCF service required methods
}
Ans:
public class Service1:IService1
{
//Implementing IService1 interface members
}
Ans: In Web API communication between service provider and service receiver will be with the
help of HTTP. Hence they are called HTTP Services.
Ans:Web API service can be accessed from all devices like Desktop, Laptop, Tablets, Mobile
devices, etc as well as Web API service can be consumed from various types of applications like
Windows Forms application, Web Application, Mobile Application.
5) Why Microsoft has introduced Web API even though it is having WCF/Web Service?
Ans:Since WCF/ XML web services requires additional protocol called SOAP, Microsoft introduced
Web API as it does not require any special protocols like SOAP, TCP, etc.
6) What is SOAP?
Ans:SOAP stands for Simple Object Access Protocol. SOAP is a xml based protocol.
Ans:HTTP protocol.
Ans:ASP.Net Web API is a new framework for building HTTP based services.
11) What is the super class for Web API controller class?
Ans:ApiController class.
Ans:System.Web.Http.ApiController
Ans:
public class ValuesController :ApiController
{
public IEnumerable<string>Get()
{
return new string[] {“value1”,”value2”};
}
public string Get(int id)
{
return “value”;
}
public void Post([FromBody] string value)
{ }
public voidPut(int id , [FromBody] string value)
{ }
public voidDelete(int id)
{ }
}
Ans:webapi.config
Ans:a) Get(),
b) Get(int id),
c) Post([FromBody] string value),
d) Put(int id,[FromBody] string value)
e) Delete(int id)
Ans:http://localhost:1234/api/values
Ans:http://localhost:1234/api/values?id=1
Ans:http://localhost:1234/api/values
Ans:http://localhost:1234/api/values?id=1
Ans:
Requires additional protocol called SOAP Does not require any special protocols like
which is xml based protocol. SOAP, TCP, etc.
It is complex to configure WCF RESTful Does not require any configuration details.
services. We can simply access by using url.
Ans:Linq stands for Language Integrated Query, which was introduced by the MicroSoft, with .Net
Framework 3.5. Linq is advanced data access object, ADO.Net is traditional data access object.
Ans:Using ADO.Net to communicate the databases within the .Net application, we will write the
SQL commands like select, insert,update,delete,create,etc.
Butusing Linq to communicate the databases within the .Net application, we don’t require to
write Sql commands. These Sql commands we can implement by using any one of the .Net
language like C#.Net or Vb.Net.
6) What is OR tool?
Ans:To communicate the datasourcesby using Linq, Microsoft is providing one predefined tool
called OR Tool (object relational).
Ans:OR tool will be providing one predefined class that predefining class will having various
predefined methods to implement remaining operations on databases like insert, update, delete
and so on.
Ans:Using Linq, we can communicate the datasources like collections, databases, xml.
9) What is the predefined method we will use for inserting the data into database by using Linq?
Ans: InsertOnSubmit().
10) What is the predefined method we will use to perform Delete,Update operation by using Linq?
Ans:Single()
Ans:It is a predefined member method of DataContext predefined class.This method will confirm
insert, delete or update operations like ‘commit’.
13) What is the attribute of RouteConfig.cs which will decide first controller?
Ans:Controller attribute
14) What is the attribute of RouteConfig.cs which will decide first action method to execute at first
request?
Ans:action attribute
Ans:Yes
Ans:Yes
Ans:
Ans:ORM stands for Object Relational Mapping. It automatically creates class which are based on
the database tables.
Ans:Find() for finding the record and SaveChanges() to update the values.
Ans:Find().
13) Write the Entity Framework code (query) syntax to fetch the data from single table.
Ans:mydbEntitiesobjdb=new mydbEntities();
var countrylist=objdb.Countries;
14) Write the Entity Framework code(query) to fetch the data from two tables(joins).
Ans:mydbEntitiesobjdb=new mydbEntities();
var loc=from c in objdb.Countriesjoins in
objdb.Statesonc.countryidequalss.countryidselectnewLocationModel
{Cname=c.countryname,Sname=s.statename};
Here Cname and Sname are properties defined inside the LocationModel class.
countryid, countryname and statename are column property names of countries and
states table properties respectively which are coming with ORM tool
LocationModel is the model class name.
15) Write the Entity Framework code(query) to fetch the data from three tables(joins).
Ans:mydbEntitiesobjdb=new mydbEntities();
var loc=from co in objdb.Countriesjoins in objdb.Statesonc.countryidequalss.countryidjoin ci in
objdb.citiesons.stateidequalsci.stateidselect newLocationModel {Cname=co.countryname,
Sname=s.statename, Ciname=ci.cityname};
Ans:mydbEntitiesobjdb=new mydbEntities();
var loc=from co in objdb.Countriesjoins in objdb.Statesonc.countryid equals s.countryidjoin ci in
objdb.citiesons.stateidequalsci.stateidjoinm in objdb.mandals on ci.cityidequalsm.cityidselect
newLocationModel {Cname=co.countryname, Sname=s.statename,
Ciname=ci.cityname,Mname=m.mandalname};
Ans:mydbEntitiesobjdb=new mydbEntities();
var loc=from co in objdb.Countriesjoins in objdb.Statesonc.countryid equals s.countryidjoin ci in
objdb.citiesons.stateidequalsci.stateidjoinm in objdb.mandals on ci.cityidequalsm.cityidjoinv in
objdb.villages on m.mandalidequalsv.mandalidselect newLocationModel
{Cname=co.countryname, Sname=s.statename,
Ciname=ci.cityname,Mname=m.mandalname,Vname=v.villagename};
1) What is bundling?
4) What is minification?
Ans: Minification Technique optimizes script or css file size by removing unnecessary white spaces and
comments and shortening variable names to one character.
Ans:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional} );
}
}
18) Which method will initiate the Routes in RouteConfig.cs file?
Ans: MapRoute
19) Write the list of available filters in mvc.
Ans: a. Authentication Filers
b. Authorization Filters
c. Action Filters
d. Result Filters
e. Exception Filters
20) What is the role authentication filters?
Ans: Authentication Filter runs before any other filter or any action method.
Anthentication confirms that valid user or invalid user
Action Filters implements the Authentication filter interace that is IAuthenticationFilter
21) What is the interface of authentication filters?
Ans: IAuthenticationFilter
22) What is the role of authorization filters?
Ans: Authorization filters are responsible for checking user access, these implements the
IAuthorizationFilter interface within framework
23) What is the interface of authorization filters?
Ans: IAuthorizationFilter
24) What is the role of Action filters?
Ans: Action Filter can apply on entire controller or single action method. This filter will be called before
and after the action has executed.
These Action Filters contain logic i.e; executed before and after a controller action executes, you
can use action filter to modify the ViewData that a controller action returns interface of Action
Filter is IActionFilter
25) What is the interface of Action filters?
Ans: IActionFilter
26) What is the role of Result filters?
Ans: The filters contains logic that is executed before and after a View result is executed that means
result filter will execute before view result and after result, interface is IResultFilter
27) What is the interface of Result filters?
Ans: IResultFilter
28) What is the role of Exception filters?
Ans: Exception filters will Exception filter to handle errors raised by either your Controller actions or
controller action results.
Interface of Exception Filter is IExceptionFilter
29) What is the interface of Exception filters?
Ans: I ExceptionFilter