Wednesday, February 25, 2015

Cannot Modify Header Information - Headers Already Sent : How To Fix

This error message gets triggered when anything is sent before you send HTTP headers (with setcookie or header). Common reasons for outputting something before the HTTP headers are:
1. Accidental whitespace, often at the beginning or end of files, like this:
the space before "php opening tag"
To avoid this, simply leave out the closing ?> - it's not required anyways.

2. "Byte order marks" at the beginning of a php file. Examine your php files with a hex editor to find out whether that's the case. They should start with the bytes 3F 3C. You can safely remove the BOM EF BB BF from the start of files.

3. Explicit output, such as calls to echo, printf, readfile, passthru, code before php opening tag etc.

4. A warning outputted by php, if the display_errors php.ini property is set. Instead of crashing on a programmer mistake, php silently fixes the error and emits a warning. While you can modify the display_errors or error_reporting configurations, you should rather fix the problem. Common reasons are accesses to undefined elements of an array (such as $_POST['input'] without using empty or isset to test whether the input is set), or using an undefined constant instead of a string literal (as in $_POST[input], note the missing quotes).

Turning on output buffering should make the problem go away; all output after the call to ob_start is buffered in memory until you release the buffer, e.g. with ob_end_flush.

However, while output buffering avoids the issues, you should really determine why your application outputs an HTTP body before the HTTP header. That'd be like taking a phone call and discussing your day and the weather before telling the caller that he's got the wrong number.
For more answers : StackOverflow

Thursday, February 19, 2015

MVC Interview Questions

What are the 3 main components of an ASP.NET MVC application?
1. M - Model
2. V - View
3. C - Controller

In which assembly is the MVC framework defined?

Is it possible to combine ASP.NET webforms and ASP.MVC and develop a single web application?
Yes, it is possible to combine ASP.NET webforms and ASP.MVC and develop a single web application.

What does Model, View and Controller represent in an MVC application?
Model: Model represents the application data domain. In short the applications business logic is contained with in the model.

View: Views represent the user interface, with which the end users interact. In short the all the user interface logic is contained with in the UI.

Controller: Controller is the component that responds to user actions. Based on the user actions, the respective controller, work with the model, and selects a view to render that displays the user interface. The user input logic is contained with in the controller.

What is the greatest advantage of using mvc over webforms?
It is difficult to unit test UI with webforms, where views in mvc can be very easily unit tested.

Which approach provides better support for test driven development - ASP.NET MVC or ASP.NET Webforms?

What are the advantages of ASP.NET MVC?
1. Extensive support for TDD. With MVC, views can also be very easily unit tested.
2. Complex applications can be easily managed
3. Seperation of concerns. Different aspects of the application can be divided into Model, View and Controller.
4. ASP.NET MVC views are light weight, as they donot use viewstate.

Is it possible to unit test an MVC application without running the controllers in an ASP.NET process?
Yes, all the features in an MVC application are interface based and hence mocking is much easier. So, we don't have to run the controllers in an ASP.NET process for unit testing.

Is it possible to share a view across multiple controllers?
Yes, put the view into the shared folder. This will automatically make the view available across multiple controllers.

What is the role of a controller in an MVC application?
The controller responds to user interactions, with the application, by selecting the action method to execute and alse selecting the view to render.

Where are the routing rules defined in an MVC application?
In Application_Start event in Global.asax

Name a few different return types of a controller action method?
The following are just a few return types of a controller action method. In general an action method can return an instance of a any class that derives from ActionResult class.
1. ViewResult
2. JavaScriptResult
3. RedirectResult
4. ContentResult
5. JsonResult

What is the significance of NonActionAttribute?
In general, all public methods of a controller class are treated as action methods. If you want prevent this default behaviour, just decorate the public method with NonActionAttribute.

What is the significance of ASP.NET routing?
ASP.NET MVC uses ASP.NET routing, to map incoming browser requests to controller action methods. ASP.NET Routing makes use of route table. Route table is created when your web application first starts. The route table is present in the Global.asax file.

What are the 3 segments of the default route, that is present in an ASP.NET MVC application?
1st Segment - Controller Name
2nd Segment - Action Method Name
3rd Segment - Parameter that is passed to the action method

Controller Name = Customer
Action Method Name = Details
Parameter Id = 5

ASP.NET MVC application, makes use of settings at 2 places for routing to work correctly. What are these 2 places?
1. Web.Config File : ASP.NET routing has to be enabled here.
2. Global.asax File : The Route table is created in the application Start event handler, of the Global.asax file.

What is the adavantage of using ASP.NET routing?
In an ASP.NET web application that does not make use of routing, an incoming browser request should map to a physical file. If the file does not exist, we get page not found error.

An ASP.NET web application that does make use of routing, makes use of URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a file, you can use URLs that are descriptive of the user's action and therefore are more easily understood by users.

What are the 3 things that are needed to specify a route?
1. URL Pattern - You can include placeholders in a URL pattern so that variable data can be passed to the request handler without requiring a query string.
2. Handler - The handler can be a physical file such as an .aspx file or a controller class.
3. Name for the Route - Name is optional.

Is the following route definition a valid route definition?
No, the above definition is not a valid route definition, because there is no literal value or delimiter between the placeholders. Therefore, routing cannot determine where to separate the value for the controller placeholder from the value for the action placeholder.

What is the use of the following default route?
This route definition, prevent requests for the Web resource files such as WebResource.axd or ScriptResource.axd from being passed to a controller.

What is the difference between adding routes, to a webforms application and to an mvc application?
To add routes to a webforms application, we use MapPageRoute() method of the RouteCollection class, where as to add routes to an MVC application we use MapRoute() method.

How do you handle variable number of segments in a route definition?
Use a route with a catch-all parameter. An example is shown below. * is referred to as catch-all parameter.

What are the 2 ways of adding constraints to a route?
1. Use regular expressions
2. Use an object that implements IRouteConstraint interface

Give 2 examples for scenarios when routing is not applied?
1. A Physical File is Found that Matches the URL Pattern - This default behaviour can be overriden by setting the RouteExistingFiles property of the RouteCollection object to true.
2. Routing Is Explicitly Disabled for a URL Pattern - Use the RouteCollection.Ignore() method to prevent routing from handling certain requests.

What is the use of action filters in an MVC application?
Action Filters allow us to add pre-action and post-action behavior to controller action methods.

If I have multiple filters implemented, what is the order in which these filters get executed?
1. Authorization filters
2. Action filters
3. Response filters
4. Exception filters

What are the different types of filters, in an mvc application?
1. Authorization filters
2. Action filters
3. Result filters
4. Exception filters

Give an example for Authorization filters in an mvc application?
1. RequireHttpsAttribute
2. AuthorizeAttribute

Which filter executes first in an mvc application?
Authorization filter

What are the levels at which filters can be applied in an mvc application?

1. Action Method
2. Controller
3. Application
[b]Is it possible to create a custom filter?[/b]

What filters are executed in the end?
Exception Filters

Is it possible to cancel filter execution?

What type of filter does OutputCacheAttribute class represents?
Result Filter

What are the 2 popular MVC view engines?
1. Razor
2. .aspx

What symbol would you use to denote, the start of a code block in razor views?

What symbol would you use to denote, the start of a code block in aspx views?
<%= %>

In razor syntax, what is the escape sequence character for @ symbol?
The escape sequence character for @ symbol, is another @ symbol

When using razor views, do you have to take any special steps to protect your mvc application from cross site scripting (XSS) attacks?
No, by default content emitted using a @ block is automatically HTML encoded to protect from cross site scripting (XSS) attacks.

When using aspx view engine, to have a consistent look and feel, across all pages of the application, we can make use of master pages. What is master pages equivalent, when using razor views?
To have a consistent look and feel when using razor views, we can make use of layout pages. Layout pages, reside in the shared folder, and are named as _Layout.cshtml

What are sections?
Layout pages, can define sections, which can then be overridden by specific views making use of the layout. Defining and overriding sections is optional.

What are the file extensions for razor views?
1. .cshtml - If the programming language is C#
2. .vbhtml - If the programming language is VB

How do you specify comments using razor syntax?
Razor syntax makes use of @* to indicate the beginning of a comment and *@ to indicate the end. An example is shown below.
@* This is a Comment *@

What are various design patterns?

What is lazy loading and how can we achieve it ?

What is TDD or Test Driven Development ?

What is Web Socket ?

Adding more soon....

You are really welcome if you have any question. Please mail me at or add a comment below.

Monday, November 10, 2014

How To Iterate a Dictionary Using Keys In C#

Dictionary is a object that can store values in Key-Value pair. its just like a list, the only difference is:
List can be iterate using index(0-n) but not the Dictionary. Generally when we try to iterate the dictionary we get below error:
"Collection was modified; enumeration operation may not execute."
So How to parse a dictionary and modify its values??
To iterate dictionary we must loop through it's keys or key - value pair.

Using keys
Loading ....

Saturday, November 1, 2014

How To Select First Option using jQuery AutoComplete

When we have a list of values and want to select one of them, we chose DropdownBox or jQuery AutoComplete for listing the element in HMTL. The modern approach to show this kind of listing is one of the most user friendly jQury Auto Complete.

When we integrate jQuery AutoComplete with minimal options it gives us facility to select any option from the list as we type in inputbox.

Here is the simplest code if we want to get this work with our input-box:
$( "#tags" ).autocomplete({
      source: availableTags

Now if we want the first list item to be selected by default when we type in some letters, here is what to do. We just have to trigger 'select' event of the Auto Complete and we are done.
$( "#tags" ).autocomplete({
      source: availableTags
And here is what we get as we type 'P' in Input Box

Happy coding :)

Friday, August 1, 2014

Select First Option of Select-Box Using Jquery

This is a simple script to select first option of a select-box using jquery
    $("#CardCountry").val($("#CardCountry option:first").val());    

Saturday, July 19, 2014

Trim Dropdown Options Using JQuery

This is the HTML of my dropdown list. I have whitespace before and after each of option for this select/ dropdown. And here is a easy way to trim these option values using jQuery each method and javascript trim method.

Tuesday, December 17, 2013

How To: Create Facebook App in 2 Minutes (With Pictures)

If you are assigned a task of working with an Facebook App and want to try-out by creating an App from scratch; Just follow these easy five Step and it will cook you a 'Five Step Facebook App'

First of all Login to Facebook, check your updates and notifications ,When you get rid of repeated photos and notification;
Go to the url :  and start creating your Five Step Facebook App.

1. First Create an App on Facebook. For this Click on 'APP' menu from top-menu and then Click on 'Create New App'

Step 1: Creating a Facebook App
Step 1: Creating a Facebook App

2. As you will set the App Name and click on 'SAVE'. It will navigate to 'Dash-Board'. You can check your APP ID and APP Secret under Dashboard.
Step 2 :  Dashboard | Facebook App
Step 2 :  Dashboard | Facebook App

3. Now Click on 'Settings' from Left menu. You will be navigated to 'BASIC' tab. Here You should do some basic preparation for your App.
Note APP ID : ***********
     APP Secret: ********** (Click on 'SHOW' will let you know your App Secret, Keep this really secret)
     App Name : You can change the App name here.
     Email : Set the email here you  want to be notified at.
Step 3 : Basic Setting For Facebook App
Step 3 : Basic Setting For Facebook App

4. And now the important part of the setting where you would set the Page as CallBack Uri or OAuth Redirect URI
(This will be a page under your domain. And Facebook will revert with Oauth Token. Let say: callback.aspx)

Step 4 : Advance Setting | Setting Callback URL for OAuth Redirection URL
Step 4 : Advance Setting | Setting Callback URL for OAuth Redirection URL

5. One more thing and we are Done with creating an App on Facebook. Just Enable(ON) the setting 'Stream post URL security' on migration page.

Step 5 : Disable 'Stream post URL security'
Step 5 : Disable 'Stream post URL security'

Click on 'Save Changes' the last time in Step 5. You have created a Facebook App Successfully.
Now You can utilize your Newly Created Facebook App from your website/Application