arşiv

Çarşamba, 07 Eyl 2016 için arşiv

MVC’de A Potentially Dangerous Request.Form Value Was Detected From The Client Hatas

Çarşamba, 07 Eyl 2016 yorum yok
[ValidateInput(false)]

 

Categories: MVC Tags:

MVC içindeki filtreler

Çarşamba, 07 Eyl 2016 yorum yok

Filter

 Interface

 Default Implementation


 Authorization Filter  IAuthorizationFilter AuthorizeAttribute
 Action Filter  IActionFilter ActionFilterAttribute
 Result Filter  IResultFilter ActionFilterAttribute
 Exception Filter  IExceptionFilter HandleErrorAttribute

Authorization : Diğer filtreler ve Action metodlardan önce çalışmaktadır.Controller ya da Controller içindeki Action metoda erişim kısıtlamak için kullanılır.
Exception : Hata durumlarında çalışan filtredir.
Action : Herhangi bir Controller sınıfında bulunan Action metottan önce ya da sonra çalışır.
Result : Action Result çalıştırılmadan önce ya da çalıştırıldıktan sonra çalışır.

 

Categories: MVC Tags:

MVC Get Attributes

Çarşamba, 07 Eyl 2016 yorum yok
Assembly assembly = Assembly.GetExecutingAssembly();
IEnumerable<Type> types = assembly.GetTypes()
	.Where(
		type =>
			typeof(Controller).IsAssignableFrom(type) &&
			type.Name.Replace("Controller", "") == controllerName)
	.OrderBy(x => x.Name);

foreach (Type cls in types)
{                
	HelpAttribute helpAttribute = Attribute.GetCustomAttribute(cls, typeof(HelpAttribute)) as HelpAttribute;
	url = helpAttribute != null ? helpAttribute.Url : string.Empty;
	........
}

 

Categories: C#, MVC, Reflection Tags: , ,

MVC Get – Controllers, Actions, Attributes and Return Types – 2

Çarşamba, 07 Eyl 2016 yorum yok
Assembly assembly = Assembly.LoadFrom(sAssemblyFileName)
IEnumerable<Type> types = assembly.GetTypes().Where(type => typeof(Controller).IsAssignableFrom(type)).OrderBy(x => x.Name);
foreach (Type cls in types)
{
      list.Add(cls.Name.Replace("Controller", ""));
      IEnumerable<MemberInfo> memberInfo = cls.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public).Where(m => !m.GetCustomAttributes(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), true).Any()).OrderBy(x => x.Name);
      foreach (MemberInfo method in memberInfo)
      {
           if (method.ReflectedType.IsPublic && !method.IsDefined(typeof(NonActionAttribute)))
           {
                  list.Add("\t" + method.Name.ToString());
           }
      }
}

 

Categories: C#, MVC, Reflection Tags: , ,

MVC Get – Controllers, Actions, Attributes and Return Types

Çarşamba, 07 Eyl 2016 yorum yok
Assembly asm = Assembly.GetAssembly(typeof(MyWebDll.MvcApplication));

var controlleractionlist = asm.GetTypes()
        .Where(type=> typeof(System.Web.Mvc.Controller).IsAssignableFrom(type))
        .SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public))
        .Where(m => !m.GetCustomAttributes(typeof( System.Runtime.CompilerServices.CompilerGeneratedAttribute), true).Any())
        .Select(x => new {Controller = x.DeclaringType.Name, Action = x.Name, ReturnType = x.ReturnType.Name, Attributes = String.Join(",", x.GetCustomAttributes().Select(a => a.GetType().Name.Replace("Attribute",""))) })
        .OrderBy(x=>x.Controller).ThenBy(x => x.Action).ToList();

 

Categories: C#, MVC, Reflection Tags: , ,

c# Mapping Class

Çarşamba, 07 Eyl 2016 yorum yok
public static class TypeExtensions
{
	public static void MapTo<T, S>(this S source, T target)
	{
		Type sourceType = source.GetType();
		Type targetType = target.GetType();

		var sourceProperties = sourceType.GetProperties();
		var targetProperties = targetType.GetProperties();

		for (int i = 0; i < sourceProperties.Length; i++)
		{
			var currentProperty = sourceProperties[i];
			var targetProperty = targetProperties
				.FirstOrDefault(p => p.Name == currentProperty.Name);
			if (targetProperty != null)
				targetProperty.SetValue(target, currentProperty.GetValue(source, null), null);
		}

	}
 
}

 

Categories: C# Tags: