Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
wanted to create custom attribute in MVC 3 . and also want to know how to invoke the attribute from controllers. help me to do this.

thanks
Posted
Updated 5-Sep-12 1:27am
v2
Comments
Sandeep Mewara 5-Sep-12 7:28am    
What kind of help you are seekin/ What have you tried so far? Where are you stuck?
Sergey Alexandrovich Kryukov 28-Sep-12 16:07pm    
I don't think this is a valid idea. -- please see my answer and OP's comment below.
--SA
sawant parag 18-Sep-12 7:52am    
in my application , i am trying to trace the end of session.

Currently when my session end , my javascript menu get disappears .


so i was planing to have some custom attributes that will run on every click on View and check whether session present or not ?
if the session expire then i will redirect that to new custom error page.
Sergey Alexandrovich Kryukov 28-Sep-12 16:07pm    
You have no idea what an attribute is. I don't think they will help you in this case. Please see my answer.
--SA

1 solution

The question is not really related to MVC. You don't "invoke" an attribute — there is no suck think. The attributes are "attributed" the the assemblies, modules, types, members and parameters as additional meta-data. When you execute any portion of your run time, they are already there. All you can do it to examine if the declarations of the programming entities mentioned above have some type of attributes; and if an attribute of certain type is found, you can read its properties. This can be done using some methods named GetCustomAttributes:
http://msdn.microsoft.com/en-us/library/system.attribute.getcustomattributes.aspx[^].

There are more methods, but I don't want to show them as they comes with most Reflection types: Assembly, Type, MethodInfo, ConstructorInfo, MemberInfo, FieldInfo, PropertyInfo, ParameterInfo and the like. You would be better off finding appropriate attribute-related methods by yourself, as you need them.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900