Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Tuesday, April 26, 2016

System.Web.Http.Authorize vs System.Web.Mvc.Authorize

You must use System.Web.Http.Authorize against an ApiController (Web API controller) and System.Web.Mvc.Authorize against a Controller (MVC controller).

Thanks to: Badri
Source: http://stackoverflow.com/a/19156530/1931848

Thursday, December 17, 2015

ASP.NET MVC Controller Lifecycle

ASP.NET MVC Controller will be executed in the following  order:

Every controller action call go through the certain events events. Some of the methods I have listed below.

1) Initialize
Executed at the beginning of action call.

protected override void Initialize(RequestContext requestContext)
{
}

2) On Authorization
Executed at the time of authorizing the action call

protected override void OnAuthorization(AuthorizationContext filterContext)
{
}

1) On Action Executing
Executed at the time of executing the action just after authorization.

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
}

Thursday, May 14, 2015

Define Display Labels as attributes in BE class in C#

Assume that in your BE class you have certain properties to match with table fields. I want to expose descriptive name for each of these properties. for example to show it as column header in grid.
For example, there is a property called FirstName. I want to expose it's descriptive name as First Name
in BE define this.
[DisplayName("First Name"), Description("First Name of the Member")]
public string FirstName
{
    get { return _firstName; }
    set { _firstName = value; }
}
You can read these details of each property as below;
PropertyDescriptorCollection propertiesCol = TypeDescriptor.GetProperties(objectBE);

PropertyDescriptor property;

for (int i = 0; i < propertiesCol.Count; i++)
{
    property = TypeDescriptor.GetProperties(objectBE)[i];

    /*
    // Access the Property Name, Display Name and Description as follows
    property.Name          // Returns "FirstName"
    property.DisplayName   // Returns "First Name"
    property.Description   // Returns "First Name of the Member"
    */
}
* where objectBE is the object instance of BE class.