public async Task CartSummaryComponent_Returns_CartedItems() { // Arrange var viewContext = new ViewContext() { HttpContext = new DefaultHttpContext() }; // Session initialization var cartId = "CartId_A"; viewContext.HttpContext.Session = new TestSession(); viewContext.HttpContext.Session.SetString("Session", cartId); // DbContext initialization var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>(); PopulateData(dbContext, cartId, albumTitle: "AlbumA", itemCount: 10); // CartSummaryComponent initialization var cartSummaryComponent = new CartSummaryComponent(dbContext) { ViewComponentContext = new ViewComponentContext() { ViewContext = viewContext } }; // Act var result = await cartSummaryComponent.InvokeAsync(); // Assert Assert.NotNull(result); var viewResult = Assert.IsType<ViewViewComponentResult>(result); Assert.Null(viewResult.ViewName); Assert.Null(viewResult.ViewData.Model); Assert.Equal(10, cartSummaryComponent.ViewBag.CartCount); Assert.Equal("AlbumA", cartSummaryComponent.ViewBag.CartSummary); }
public string GetLayoutName(ViewContext viewContext) { ISiteSettings site = siteResolver.Resolve(); if (site == null) return options.DefaultLayout; string layout = options.DefaultLayout; //"_Layout" // resolve tenant specific layout file name if (options.SelectionMode == LayoutSelectionMode.Convention) // this is the default for now { // we could use a convention like Site1Layout.cshtml Site2Layout.cshtml // based on siteid layout = string.Format(CultureInfo.InvariantCulture, options.ConventionFormat, site.SiteId.ToInvariantString()); } else { // we could store a layout name in ISiteSettings.Skin // in that case we would need a way to browse layout files from the UI // and tricky logic or conventions to filter to only layout files so no other views can be chosen // then if we do that would we need a way to make each tenant not be able to select // a layout that belongs to another tenant? // generally I think of multi-tenant for multiple sites owned by the same customer // so in that case it is not a big issue for one tenant to see the other tenant layouts // need a way to enumerate layout views in the file system to populate a dropdown // currently there is no ui for selecting a layout so you would have to set it in the db // in mp_Sites.Skin field if(site.Skin.Length > 0) { layout = site.Skin.Replace(".cshtml", string.Empty); } } // in all cases we need to determine of the layout file exists // and if not log something and fallback to a known layout file var layoutPageResult = viewEngine.FindPage(viewContext, layout); if (layoutPageResult.Page == null) { log.LogError("could not find the layout " + layout); return options.DefaultLayout; } else { //log.LogInformation("found the layout " + layout); return layout; } }
/// <summary> /// Creates a new <see cref="ViewComponentContext"/>. /// </summary> /// <param name="viewComponentDescriptor"> /// The <see cref="ViewComponentContext"/> for the View Component being invoked. /// </param> /// <param name="arguments">The View Component arguments.</param> /// <param name="viewContext">The <see cref="ViewContext"/>.</param> /// <param name="writer">The <see cref="TextWriter"/> for writing output.</param> public ViewComponentContext( [NotNull] ViewComponentDescriptor viewComponentDescriptor, [NotNull] object[] arguments, [NotNull] ViewContext viewContext, [NotNull] TextWriter writer) { ViewComponentDescriptor = viewComponentDescriptor; Arguments = arguments; // We want to create a defensive copy of the VDD here so that changes done in the VC // aren't visible in the calling view. ViewContext = new ViewContext( viewContext, viewContext.View, new ViewDataDictionary(viewContext.ViewData), writer); }
public async Task ExecuteResultAsync(RequestContext context) { if (context == null) { throw new ArgumentNullException("context"); } IView view = await FindView(context, ViewName); using (view as IDisposable) { context.HttpContext.Response.ContentType = "text/html"; using (var writer = new StreamWriter(context.HttpContext.Response.Body, Encoding.UTF8, 1024, leaveOpen: true)) { var viewContext = new ViewContext(context.HttpContext, context.RouteData, ViewData) { ServiceProvider = _serviceProvider }; await view.RenderAsync(viewContext, writer); } } }
public string GetLayoutName(ViewContext viewContext) { return options.DefaultLayout; }
/// <summary> /// Creates a new <see cref="ViewComponentContext"/>. /// </summary> /// <remarks> /// The default constructor is provided for unit test purposes only. /// </remarks> public ViewComponentContext() { ViewComponentDescriptor = new ViewComponentDescriptor(); Arguments = new object[0]; ViewContext = new ViewContext(); }
internal static IView FindPartialView(ViewContext viewContext, string partialViewName, IViewEngine viewEngine) { ViewEngineResult result = viewEngine.FindView(viewContext, partialViewName).Result; if (result.View != null) { return result.View; } StringBuilder locationsText = new StringBuilder(); foreach (string location in result.SearchedLocations) { locationsText.AppendLine(); locationsText.Append(location); } throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "MvcResources.Common_PartialViewNotFound", partialViewName, locationsText)); }