Monday, July 28, 2014

Exploring Abstract class (OOP concepts) in .NET - My Observations

Following are my observations with Abstract classes in .NET.
Feel free to write in comments if you think of more points to be added in this list, or if you want know more details about one or more points :)

  1. A class must be declared with "abstract" keyword
  2. It can contain complete implantation of methods
  3. Methods declared with "abstract" keyword can contain only declarations in Abstract class
  4. We cannot create an instance of abstract class
  5. Abstract class must be inherited
  6. A class derived from an abstract class must contain complete implementation of all abstract methods
  7. An abstract class can be derived from another abstract class
  8. Abstract class can be used as a parameter in methods
  9. If a class contains an abstract method, then that class must be marked as "abstract"
  10. An abstract class can have a constructor
  11. An abstract class can be inherited from a non-abstract class
  12. Constructor rules are also applied in case of abstract classes inheritance

P.S. - Please note all keyword and code snippets (if demonstrated above) are in C#.NET 

Thursday, July 24, 2014

HTML/ CSS: FIX >> Contents do not appear on new line after floated DIVs.

When we use "float:left", or "float:right" styles in DIVs, we come across an issue due to which the contents after those floated DIVs is not appearing in new line, rather they start appearing immediately besides the completion of last floated DIV.

In order to make further contents appearing in new line, add following DIV after floated DIVs:

<div style="clear: both;"></div>

How constructors are called while the classes are inherited: Exploring Object Oriented Programming and Concepts

Constructor Calls in Inheritance

Case study 1:

ClassA is a base class of ClassB.
Both classes have default constructor.

Observations:
While creating a new instance of ClassB (without parameters):
- it will first call default constructor of ClassB which will call constructor of ClassA.
- it will complete executing code of default constructor of ClassA.
- after that, it will complete executing code of default constructor of ClassB.

Case study 2:

ClassA is a base class of ClassB.
ClassA has no default constructor, but only parameterized constructor.
ClassB has a default constructor.


Observations: 
- it will not allow creating ClassA without a default constructor 
- if you want to do so, you need to call a base class constructor from ClassB's constructor: 
For example, following is the constructor of class A 
public ClassA(string someVariable) ..... some code .... }  
To allow ClassA without a default constructor, its all child classes should call base class constructor as below: 
public ClassB(): base("some value") 
or
public ClassB(string fullName): base(fullName)

Thursday, July 17, 2014

MVC Attribute to restrict users to enter any kind of HTML tags in the input fields

By default, if a user tries to enter any HTML tags in any of the input boxes in an MVC website, it will throw an error saying "Potentially dangerous request".
This is OK, as it doesn't allow user to enter HTML tags and prevents the site from security threat.

However, the error page is not user friendly, and it is not giving a clue to the user as what was wrong there. So you may need to avoid default error page of browser, and instead your custom validation message should be displayed.

Also, there may be circumstances when you want to allow some fields to accept HTML tags (such as rich text boxes), and rest of the input fields shouldn't allow HTML. In that case, this approach will help.

In order to achieve this we need to create an attribute in MVC and then can decorate Model properties with that attribute.

Step 1
- Decorate HttpPost method with [ValidateInput(false)] 
Validate input is true by default, and that is why it prevents execution of any code in case if there is any HTML input values are detected in posted data.
Here, we need to set it to false" as we would like to execute code of our custom attribute       

[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public virtual ActionResult CreateCustomer(Customer viewModel)
{
......
}


Step 2
- Create custom attribute as below:

using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.Text.RegularExpressions;
public class DenyHtmlInputAttribute: ValidationAttribute
{


protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value == null)
return ValidationResult.Success;


var tagWithoutClosingRegex = new Regex(@"<[^>]+>");


var hasTags = tagWithoutClosingRegex.IsMatch(value.ToString());


if (!hasTags)
return ValidationResult.Success;

return new ValidationResult(String.Format("{0} cannot contain html tags", validationContext.DisplayName));
}


}
Step 3 
- Decorate all input fields of your Model with the attribute created above:
[DenyHtmlInput]
public string Comments { get; set; }
That's it.

This is how you can restrict Html inputs on specific fields of your MVC view.

Tuesday, July 15, 2014

FIX: CKEditor not showing up when site is deployed on IIS server (ckeditor-full version 4.4.2)

Scenario:

Recently, I came across an issue due to which CKEditor was not showing up after deploying website in IIS. It was showing an empty placeholder instead. It was showing up completely fine in development environment, and while running site through Visual Studio.

I had "ckeditor-full" (Version 4.4.2)" package added in my MVC project, and also bundles to load "\ckeditor\adapters\jquery.js" and "ckeditor\ckeditor.js" javascript files in the project.

I checked in the code but it all looked fine.

As a resolution, it turned out that, I had to include following line in my view before loading the bundles -

<script type="text/javascript">
    CKEDITOR_BASEPATH = "@Url.Content("~/Scripts/ckeditor/")";
</script>

Hope this will help someone.