What is Bundling and Minification in ASP.NET MVC?
Bundling
In normal scenario let say for an instance,
- User makes a request to index action in customer controller.
- Action method will return a view result.
- Now the view contains reference to 3 js files and 3 css files.
- When browser receives the view in response, it comes to know about these references so it will make 6 more requests to server asking for the referenced files.
It
will affect the overall response time of the view. To get one complete
view browser makes 7 requests (one for view, 3 for js and 3 for css).
Now imagine the situation when we have more number of css and js files. 

Bundling is the solution for the same.
It
let us combine multiple files into one file at runtime. Using this
concept we can create bundles of js and css files. Fewer files mean
fewer response times and it means quick response time.
Minification
Normally how we write or JavaScript and css. We follow following rules normally while writing css or js.
- We use proper indentation
- We properly add spaces
- We use enter properly
- We use meaning full names for defining variables
- Write comments wherever we write complex logic
What next?
It will affect the size of the file. File size will be increased and thus increased download time.
It will affect the size of the file. File size will be increased and thus increased download time.
Solution
is Minification. Minification is a technology which will reduces the
size of the file at runtime by removing unnecessary whitespaces,
comments and shortening variable names.
How to perform bundling and Minification?
Step 1: Add reference of System.Web.Optimization to your project
Step 2: In global.asax create the bundles as follows
BundleTable.Bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-2.1.1.js", "~/Scripts/jquery.validate.js",
"~/Scripts/jquery.validate.unobtrusive.js"));
Step 3: Enable optimization in global.asaxasfollws
BundleTable.EnableOptimizations = true;
Step 4: In the view include bundle as follows
@Scripts.Render("~/bundles/jquery")

-----------------
Explain MVC OAuth ?
This
MVC Interview question is also asked in a different way “Can we
integrate FB and Twitter account authorization with our MVC Application.
One
of the most boring process for an end user is registering on a site.
Sometimes those long forms and email validation just puts off the user.
So how about making things easy by validating the users using their
existing facebook / twitter / linkedin / etc accounts. So the user
uses something which he already has while the site is assured that this
user is a proper user.
This is achieved by using MVC OAuth (Open standard for authorization).

Using MVC Oauth is a three step process:-
- Register your MVC application / website with the external site i.e. facebook , twitter etc. Once you register your app you get an ID and key from the external site. Below is snap shot of how facebook gives the ID and Key. In FB they term the key as the “App secret”. This process varies from site to site. So FB can have X steps while twitter can X+1.

- Next step is to open “AuthConfig.cs” and you will see lot of readymade code to user your ID and Key. So use the appropriate method as per site and provide the ID and Key.
OAuthWebSecurity.RegisterMicrosoftClient(
clientId: "",
clientSecret: "");
OAuthWebSecurity.RegisterTwitterClient(
consumerKey: "",
consumerSecret: "");
OAuthWebSecurity.RegisterFacebookClient(
appId: "",
appSecret: "");
OAuthWebSecurity.RegisterGoogleClient();
- Now if you run your application you should get a link to FB login as shown in the below screen. So if you login in FB it will redirect to your MVC application.

ASP.NET MVC written test Interview question
Below is a MVC written test Interview question paper which was given to senior people to do pre-evaluation.
If you are supposed to do validation in MVC what the best approach?
- Write the validation in the models.
- Use data annotations like [required] validator.
You want to implement Ajax in MVC, what will you do?
- XML HTTP Request
- Use JSON and JQUERY calls
- Update panel
You want to create SEO friendly URL’s in MVC?
- Routing
- Create custom HTTP handlers and use VERB
Which of the below statements are false?
- Viewdata helps to pass data from controller to view.
- Tempdata helps to pass data from controller to controller.
- Viewdata helps to pass data from view to controller.
If you want to bind two models with a view?
- Not possible with MVC
- Use ViewModel
You want to emit out JSON from MVC?
- Serialize the object and pass it back
- Use JSONResult
- Use WCF and emit out JSON
How is the hierarchy in Angular?
- Controller -> App -> Model
- App -> Controller -> Model
You want to compress your JavaScript files in MVC?
- Use Winrar
- Use Bundling and minification
You want to make cross domain JSON calls :-
- Enable cross domain in web.config file of JSON project.
- Use [CrossDomain] attribute.
- CORS does not work by design
What is the use of filters ?
- Filters and queries SQL
- Does preprocessing and post processing logic
Can we do pessimistic locking in EF ?
- Yes
- No
You want to render MVC ASCX ?
- Use RenderAction
- Use RenderPartial
Is it possible to create a custom view engine like Razor ?
- No
- Yes
How to enable Lazy loading in EF ?
- Use include
- Use stored procedures.
- None of the above , it enabled by default.
What is false about WebAPI ?
- It does content negotiation.
- Emits out JSON and XML
- Emits out WCF SOAP
You have heavy Action which is taking lot of time ?
- Create that site on a different app pool.
- Use Delegates
- Use Async controllers.
-----------------
----------------
----------------
--------------
-------------
Explain the difference between layout and master pages ? ( MVC Razor interview questions)
Layout
are like master pages in ASP.NET Web form. Master pages give a standard
look and feel for Web form views while layout gives standard look and
feel or acts like a template for razor views.
How to apply layout to Razor views?
So first we need to create a template file as shown in the below code.

And then apply this template to the view as shown below and display data in those respective sections.

You can go through these 100 MVC interview question with answers fromhttp://www.codeproject.com/Articles/556995/MVC-interview-questions-with-answers
Explain MVC model binders ? ( ASP.NET MVC interview questions)
June 13, 2014 at 1:01pm
Model binder maps HTML form elements to the model. It acts like a bridge between HTML UI and MVC model.

Take the below simple HTML form example :-
<formid="frm1" method=post action=”/Customer/SubmitCustomer”>
Customer code :- <inputname="CCode"type="text"/>
Customer name :- <inputname="CName"type="text"/>
<input type=submit/>
</form>
Now
this form needs to fill the below “Customer” class model. If you see
the HTML control name they are different from the class property name.
For example HTML textbox control name is “CCode” and the class property
name is “CustomerCode”. This mapping code is written in HTML binder
classes.
publicclassCustomer
{
publicstring CustomerCode { get; set; }
publicstring CustomerName { get; set; }
}
To
create a model binder we need to implement “IModelBinder” interface and
mapping code needs to be written in the “BindModel” method as shown in
the below code.
publicclassCustomerBinder : IModelBinder
{
publicobject BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
HttpRequestBase request = controllerContext.HttpContext.Request;
string strCustomerCode = request.Form.Get("CCode");
string strCustomerName = request.Form.Get("CName");
returnnewCustomer
{
CustomerCode = strCustomerCode,
CustomerName = strCustomerName
};
}
}
Now in the action result method we need to use the “ModelBinder” attribute which will attach the binder with the class model.
publicActionResult SubmitCustomer([ModelBinder(typeof(CustomerBinder))]Customer obj)
{
return View(“DisplayCustomer”);
}
This
MVC interview question was provided by questpond which conducts ASP.NET
MVC training . In case you are new to MVC you can start from questpond
video which is shown below.
MVC and .NET learning tutorials for components of MVC and how do you perform unit testing in MVC?
Components of MVC: -
MVC consist of Model, View and Controller.
- View represents UI of the application with which end user interacts.
- Model represents the Business Data and business Logic
- Controller handles the User Interaction logic.
Unit testing: -
Unit
testing means to test each and every small logic in our code in an
independent and automated manner. Also see tutorial video on .NET
explaining practically on Simple Unit Testing: -
Performing unit testing in MVC: -
When
we say logic there are different kinds of logic. Business Logic,
Database logic, User Interaction Logic, Presentation logic and Data
Transformation logic,
- In MVC Model or Business Logic, is simply a class with some functions. So unit testing of it is not a big deal. It can be done easily using traditional class library unit testing approach.
- User interaction logic resides in Controller which is also a simple class. It can be tested with no much effort. Click herehttp://www.codeproject.com/Articles/763928/MVC-Unit-Testing-Unleashed#TestingInMVC to read more about controller unit testing
- Database logic location will be different for different people. Some people put them in the Model whereas some consider it as a separate layer in an application. In either case it’s going to be simple class library and hence falls under traditional class library unit testing.
- Remaining is Presentation Logic and Data Transformation logic. Best example of presentation logic is displaying different controls in different colours based on some dynamic value. Data Transformation logic means, converting format of data to be displayed. Example we have DateOBirth and we want to display age.
Such kind of logic always need to be written inside the view. Unit testing of view is not an easy thing.
Solution is Implement ViewModel concept and take out these two logic from view and put it as a separate class.
-------------
No comments:
Post a Comment