private static ViewComponentContext GetViewComponentContext(IView view, Stream stream) { var actionContext = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor()); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var viewContext = new ViewContext(actionContext, view, viewData, null, TextWriter.Null); var writer = new StreamWriter(stream) { AutoFlush = true }; var viewComponentDescriptor = new ViewComponentDescriptor() { Type = typeof(object), }; var viewComponentContext = new ViewComponentContext(viewComponentDescriptor, new object[0], viewContext, writer); return(viewComponentContext); }
/// <inheritdoc /> public async Task ExecuteAsync([NotNull] ViewComponentContext context) { string qualifiedViewName; if (ViewName != null && ViewName.Length > 0 && ViewName[0] == '/') { // View name that was passed in is already a rooted path, the view engine will handle this. qualifiedViewName = ViewName; } else { // This will produce a string like: // // Components/Cart/Default // // The view engine will combine this with other path info to search paths like: // // Views/Shared/Components/Cart/Default.cshtml // Views/Home/Components/Cart/Default.cshtml // Areas/Blog/Views/Shared/Components/Cart/Default.cshtml // // This supports a controller or area providing an override for component views. qualifiedViewName = string.Format( CultureInfo.InvariantCulture, ViewPathFormat, ViewComponentConventions.GetComponentName(context.ComponentType), ViewName ?? "Default"); } var view = FindView(context.ViewContext, qualifiedViewName); var childViewContext = new ViewContext( context.ViewContext, view, ViewData ?? context.ViewContext.ViewData, context.Writer); using (view as IDisposable) { await view.RenderAsync(childViewContext); } }
private static ViewComponentContext GetViewComponentContext(IView view, ViewDataDictionary viewData, object diagnosticListener = null) { var diagnosticSource = new DiagnosticListener("Microsoft.AspNet"); if (diagnosticListener == null) { diagnosticListener = new TestDiagnosticListener(); } diagnosticSource.SubscribeWithAdapter(diagnosticListener); var serviceProvider = new Mock <IServiceProvider>(); serviceProvider.Setup(s => s.GetService(typeof(DiagnosticSource))).Returns(diagnosticSource); var httpContext = new DefaultHttpContext(); httpContext.RequestServices = serviceProvider.Object; var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); var viewContext = new ViewContext( actionContext, view, viewData, new TempDataDictionary(new HttpContextAccessor(), new SessionStateTempDataProvider()), TextWriter.Null, new HtmlHelperOptions()); var viewComponentDescriptor = new ViewComponentDescriptor() { Type = typeof(object), }; var viewComponentContext = new ViewComponentContext( viewComponentDescriptor, new object[0], viewContext, TextWriter.Null); return(viewComponentContext); }
/// <summary> /// Renders JSON text to the output. /// </summary> /// <param name="context">The <see cref="ViewComponentContext"/>.</param> public void Execute([NotNull] ViewComponentContext context) { var serializerSettings = _serializerSettings; if (serializerSettings == null) { serializerSettings = context .ViewContext .HttpContext .RequestServices .GetRequiredService <IOptions <MvcJsonOptions> >() .Options .SerializerSettings; } using (var jsonWriter = new JsonTextWriter(context.Writer)) { jsonWriter.CloseOutput = false; var jsonSerializer = JsonSerializer.Create(serializerSettings); jsonSerializer.Serialize(jsonWriter, Value); } }
private static ViewComponentContext GetViewComponentContext(IView view, ViewDataDictionary viewData) { var actionContext = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor()); var viewContext = new ViewContext( actionContext, view, viewData, null, TextWriter.Null, new HtmlHelperOptions()); var viewComponentDescriptor = new ViewComponentDescriptor() { Type = typeof(object), }; var viewComponentContext = new ViewComponentContext( viewComponentDescriptor, new object[0], viewContext, TextWriter.Null); return(viewComponentContext); }
#pragma warning disable 1998 public async Task ExecuteAsync([NotNull] ViewComponentContext context) { Execute(context); }
private static JsonOutputFormatter ResolveFormatter(ViewComponentContext context) { var services = context.ViewContext.HttpContext.RequestServices; return(services.GetRequiredService <JsonOutputFormatter>()); }
/// <summary> /// Locates and renders a view specified by <see cref="ViewName"/>. If <see cref="ViewName"/> is <c>null</c>, /// then the view name searched for is<c>"Default"</c>. /// </summary> /// <param name="context">The <see cref="ViewComponentContext"/> for the current component execution.</param> /// <remarks> /// This method synchronously calls and blocks on <see cref="ExecuteAsync(ViewComponentContext)"/>. /// </remarks> public void Execute([NotNull] ViewComponentContext context) { var task = ExecuteAsync(context); TaskHelper.WaitAndThrowIfFaulted(task); }
/// <summary> /// Renders JSON text to the output. /// </summary> /// <param name="context">The <see cref="ViewComponentContext"/>.</param> public void Execute([NotNull] ViewComponentContext context) { var formatter = Formatter ?? ResolveFormatter(context); formatter.WriteObject(context.Writer, Value); }
/// <summary> /// Renders JSON text to the output. /// </summary> /// <param name="context">The <see cref="ViewComponentContext"/>.</param> /// <returns>A completed <see cref="Task"/>.</returns> public Task ExecuteAsync([NotNull] ViewComponentContext context) { Execute(context); return(Task.FromResult(true)); }
public void Execute([NotNull] ViewComponentContext context) { var formatter = new JsonOutputFormatter(); formatter.WriteObject(context.Writer, Data); }
public void Execute([NotNull] ViewComponentContext context) { throw new NotImplementedException("There's no support for syncronous views right now."); }
/// <summary> /// Writes the <see cref="EncodedContent"/>. /// </summary> /// <param name="context">The <see cref="ViewComponentContext"/>.</param> public void Execute([NotNull] ViewComponentContext context) { context.Writer.Write(EncodedContent.ToString()); }
private static IViewEngine ResolveViewEngine(ViewComponentContext context) { return(context.ViewContext.HttpContext.RequestServices.GetRequiredService <ICompositeViewEngine>()); }
public void Execute([NotNull] ViewComponentContext context) { var formatter = new JsonOutputFormatter(SerializerSettings, Indent); formatter.WriteObject(context.Writer, Data); }
private static JsonOutputFormatter ResolveFormatter(ViewComponentContext context) { var services = context.ViewContext.HttpContext.RequestServices; return services.GetRequiredService<JsonOutputFormatter>(); }
private async Task InvokeCoreAsync([NotNull] TextWriter writer, [NotNull] Type componentType, object[] args) { var invoker = _invokerFactory.CreateInstance(componentType.GetTypeInfo(), args); if (invoker == null) { throw new InvalidOperationException( Resources.FormatViewComponent_IViewComponentFactory_ReturnedNull(componentType)); } var context = new ViewComponentContext(componentType.GetTypeInfo(), _viewContext, writer); await invoker.InvokeAsync(context); }
/// <summary> /// Locates and renders a view specified by <see cref="ViewName"/>. If <see cref="ViewName"/> is <c>null</c>, /// then the view name searched for is<c>"Default"</c>. /// </summary> /// <param name="context">The <see cref="ViewComponentContext"/> for the current component execution.</param> /// <remarks> /// This method synchronously calls and blocks on <see cref="ExecuteAsync(ViewComponentContext)"/>. /// </remarks> public void Execute([NotNull] ViewComponentContext context) { var task = ExecuteAsync(context); task.GetAwaiter().GetResult(); }
public void Execute(ViewComponentContext context) { }
/// <summary> /// Writes the <see cref="EncodedContent"/>. /// </summary> /// <param name="context">The <see cref="ViewComponentContext"/>.</param> /// <returns>A completed <see cref="Task"/>.</returns> public async Task ExecuteAsync([NotNull] ViewComponentContext context) { await context.Writer.WriteAsync(EncodedContent.ToString()); }
public Task ExecuteAsync(ViewComponentContext context) { return Task.FromResult(0); }