本文整理汇总了C#中System.Web.Mvc.ControllerContext.GetActionDescriptor方法的典型用法代码示例。如果您正苦于以下问题:C# ControllerContext.GetActionDescriptor方法的具体用法?C# ControllerContext.GetActionDescriptor怎么用?C# ControllerContext.GetActionDescriptor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Web.Mvc.ControllerContext
的用法示例。
在下文中一共展示了ControllerContext.GetActionDescriptor方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BindModel
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var modelType = ModelBinders.Binders[typeof(Type)].BindModel(controllerContext, bindingContext) as Type;
var index = int.Parse(controllerContext.RouteData.Values["index"].ToString());
var actionName = (string)controllerContext.RouteData.Values["action"];
var mapping = GetMethodMapping(controllerContext, modelType, actionName, index);
var method = new Method(new MethodDescriptor(mapping, controllerContext.GetActionDescriptor(actionName)));
foreach (var parameter in method.Parameters)
{
var result = bindingContext.ValueProvider.GetValue(parameter.Name);
if (result != null)
{
if (!parameter.IsModel)
parameter.Value = result.ConvertTo(parameter.MemberType);
else
{
var context = ModelAssemblies.GetContext(parameter.MemberType);
var descriptor = new ModelDescriptor(ModelMappingManager.FindByType(parameter.MemberType));
var value = result.ConvertTo(descriptor.KeyProperty.PropertyType);
parameter.Value = context.Set(parameter.MemberType).Find(value);
}
}
}
return method;
}
示例2: BindModel
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var actionName = (string)controllerContext.RouteData.Values["action"];
var mapping = GetMethodMapping(controllerContext, bindingContext, actionName);
var method = new Method(new MethodDescriptor(mapping, controllerContext.GetActionDescriptor(actionName)));
foreach (var parameter in method.Parameters)
{
ValueProviderResult result;
if (parameter.IsModelCollection)
{
var type = typeof(List<>).MakeGenericType(parameter.UnderliningModel.ModelType);
var collection = Activator.CreateInstance(type);
var index = 0;
while ((result = bindingContext.ValueProvider.GetValue(string.Format("{0}[{1}]", parameter.Name, index))) != null)
{
var item = ModelBinder.GetModelValue(result, parameter.UnderliningModel.ModelType) ?? Activator.CreateInstance(parameter.UnderliningModel.ModelType);
var itemModel = new Model(parameter.UnderliningModel.ModelType, parameter.UnderliningModel.Descriptor, item);
foreach (var itemProperty in itemModel.Properties)
{
var itemPropertyName = string.Format("{0}[{1}].{2}", parameter.Name, index, itemProperty.Name);
var itemResult = bindingContext.ValueProvider.GetValue(itemPropertyName);
if (itemResult != null)
{
itemProperty.Value = !itemProperty.IsModel
? ModelBinder.GetNonModelValue(itemResult, itemProperty.MemberType)
: ModelBinder.GetModelValue(itemResult, itemProperty.MemberType);
}
}
type.GetMethod("Add").Invoke(collection, new[] { item });
index++;
}
parameter.Value = collection;
}
else
{
result = bindingContext.ValueProvider.GetValue(parameter.Name);
if (result == null)
{
controllerContext.HttpContext.Request.InputStream.Position = 0;
var provider = new JsonValueProviderFactory().GetValueProvider(controllerContext);
if (provider != null)
result = provider.GetValue(parameter.Name);
}
if (result != null)
{
parameter.Value = !parameter.IsModel
? ModelBinder.GetNonModelValue(result, parameter.MemberType)
: ModelBinder.GetModelValue(result, parameter.MemberType);
}
}
}
return method;
}