private TAttribute GetAttribute <TAttribute>(ViewSourceContext context) where TAttribute : Attribute { var responseType = context.ActionDescriptor.Route.ResponseType; var action = context.ActionDescriptor.Action; return(responseType?.GetAttribute <TAttribute>() ?? action.GetActionOrHandlerAttribute <TAttribute>()); }
public static string[] DefaultViewNameConvention(ViewSourceContext context) { var viewNames = new List <string>(); var respponseTypeName = context.ActionDescriptor.Route.ResponseType?.Type .GetNestedName().GetNonGenericName().NormalizeNestedTypeName(); if (respponseTypeName.IsNotNullOrEmpty()) { viewNames.Add(respponseTypeName); } viewNames.Add(context.ActionDescriptor.Action.HandlerTypeDescriptor.Type .GetNestedName().GetNonGenericName().NormalizeNestedTypeName()); return(viewNames.ToArray()); }
protected override ViewDescriptor[] GetViewDescriptors(ViewSourceContext context, string[] viewNames, Encoding encoding) { var handlerDescriptor = context.ActionDescriptor.Action .HandlerTypeDescriptor; var @namespace = handlerDescriptor.Type.Namespace ?? ""; var resources = handlerDescriptor.AssemblyDescriptor.Resources; return(context.SupportedTypes .SelectMany(t => viewNames.Select(v => new { Resource = resources.FirstOrDefault(x => x.Name .EqualsUncase($"{@namespace}.{v}.{t}")), Type = t })) .Where(x => x.Resource != null) .Select(x => new ViewDescriptor(x.Type, () => x.Resource.GetString(encoding))) .ToArray()); }
public virtual View[] GetViews(ViewSourceContext context) { var acceptTypes = GetAttribute <ViewAcceptAttribute>(context)?.Accept ?? _viewConfiguration.DefaultAcceptTypes.ToArray(); var contentType = GetAttribute <ViewContentTypeAttribute>(context)?.ContentType ?? _viewConfiguration.DefaultContentType; var encoding = GetAttribute <ViewEncodingAttribute>(context)?.Encoding ?? _viewConfiguration.DefaultEncoding; var viewNames = GetAttribute <ViewAttribute>(context)?.Names ?? (_viewConfiguration.ViewNameConvention ?? DefaultViewNameConvention).Invoke(context); return(GetViewDescriptors(context, viewNames, encoding)? .Select(x => new View(x.Type, x.Source, acceptTypes, encoding, contentType, context.ActionDescriptor)) .ToArray() ?? EmptyViews); }
protected override ViewDescriptor[] GetViewDescriptors( ViewSourceContext context, string[] viewNames, Encoding encoding) { var webRoot = _pathProvider.MapPath("~/"); var @namespace = context.ActionDescriptor.Action .HandlerTypeDescriptor.Type.Namespace ?? ""; return(_viewConfiguration .NamespacePathMappings .Where(m => m.Applies(@namespace)) .Select(m => m.Map(@namespace, Path.DirectorySeparatorChar)) .SelectMany(x => context.SupportedTypes .SelectMany(t => viewNames.Select(v => new { Path = Path.Combine(webRoot, x, $"{v}.{t}"), Type = t }))) .Where(x => File.Exists(x.Path)) .Select(x => new ViewDescriptor(x.Type, () => File.ReadAllText(x.Path, encoding))) .ToArray()); }
protected abstract ViewDescriptor[] GetViewDescriptors( ViewSourceContext context, string[] viewNames, Encoding encoding);
public virtual bool AppliesTo(ViewSourceContext context) { return(context.ActionDescriptor.Route.HasResponse); }