asp.net-core-viewcomponent

Where should I include a script for a view component?

六月ゝ 毕业季﹏ 提交于 2019-12-04 04:58:01
I have tried adding a section script inside a view component's view. @section scripts { <script src="~/somepath" asp-append-version="true"></script> } I also have the Render Section in the shared layout @RenderSection("scripts", required: false) When used in partial views and elsewhere in the project the script loads fine. However when in a View Component the script does not load. I suppose I could include the script in the section tag of every view that calls the component. I feel this does not fit with the self contained nature of a view component. Is there another way I can do this? I also

Is it possible to call ViewComponent from custom TagHelper?

核能气质少年 提交于 2019-12-03 04:03:12
I'm writing a custom TagHelper and want to render a ViewComponent inside it. Something similar to what vc:xyz tag helper does, but in a more controlled way, so that I can determine at runtime which ViewComponent to render. Is it possible? In order to do that, you need to inject IViewComponentHelper into your TagHelper, contextualize it and then use it to render any ViewComponent depending on your application logic. Here is a quick illustration: [HtmlTargetElement("widget", Attributes = WidgetNameAttributeName)] public class WidgetTagHelper : TagHelper { private const string

ASP.NET MVC 6: view components in a separate assembly

眉间皱痕 提交于 2019-11-28 18:48:48
I'd like to define view components (which are new in ASP.NET MVC 6) in a separate assembly from the MVC 6 web startup project so that I can reuse them in multiple web projects. A sample solution might look like this: BookStore.Components (houses common view components) BookStore.Web1 (references BookStore.Components) BookStore.Web2 (references BookStore.Components) I created a new Class Library (Package) and created a view component inside. I also created the view following the nested folder convention. My BookStore.Components project looks like this: When I try to invoke this view component

Viewcomponent alternative for ajax refresh

二次信任 提交于 2019-11-28 09:05:01
I have a viewcomponent that contains some reusable business logic that embed in various pages. This has been working fine. However, I now have a requirement to refresh the viewcomponent using ajax. Is there any way to accomplish this? From what I have read, it is not possible, although that info was a bit outdated. If it is not possible, what is the best alternative? Daniel J.G. On beta7 it is now possible to return a ViewComponent directly from a controller. Check the MVC/Razor section of the announcement The new ViewComponentResult in MVC makes it easy to return the result of a ViewComponent

How to unit test ViewComponent.Invoke()?

旧街凉风 提交于 2019-11-28 01:55:11
In ViewComponent object, HttpContext and User are read-only properties. How to unit test such a component? I'm using the MSTest Freamwork. The follow properties are used in my code Cookie Session User(System.Security.Principal) public ViewViewComponentResult Invoke() { var vm = new SummaryViewModel(); if (User.Identity is ClaimsIdentity identity && identity.IsAuthenticated) { vm.IsAuthenticated = true; vm.UserName = identity.Claims.FirstOrDefault(c => c.Type == "UserName").Value; vm.PhotoUrl = identity.Claims.FirstOrDefault(c => c.Type == "FacePicture").Value; } return View(vm); } [TestMethod]

ASP.NET MVC 6: view components in a separate assembly

我们两清 提交于 2019-11-27 11:17:16
问题 I'd like to define view components (which are new in ASP.NET MVC 6) in a separate assembly from the MVC 6 web startup project so that I can reuse them in multiple web projects. A sample solution might look like this: BookStore.Components (houses common view components) BookStore.Web1 (references BookStore.Components) BookStore.Web2 (references BookStore.Components) I created a new Class Library (Package) and created a view component inside. I also created the view following the nested folder

Viewcomponent alternative for ajax refresh

泪湿孤枕 提交于 2019-11-27 02:38:57
问题 I have a viewcomponent that contains some reusable business logic that embed in various pages. This has been working fine. However, I now have a requirement to refresh the viewcomponent using ajax. Is there any way to accomplish this? From what I have read, it is not possible, although that info was a bit outdated. If it is not possible, what is the best alternative? 回答1: On beta7 it is now possible to return a ViewComponent directly from a controller. Check the MVC/Razor section of the

How to unit test ViewComponent.Invoke()?

左心房为你撑大大i 提交于 2019-11-26 17:24:05
问题 In ViewComponent object, HttpContext and User are read-only properties. How to unit test such a component? I'm using the MSTest Freamwork. The follow properties are used in my code Cookie Session User(System.Security.Principal) public ViewViewComponentResult Invoke() { var vm = new SummaryViewModel(); if (User.Identity is ClaimsIdentity identity && identity.IsAuthenticated) { vm.IsAuthenticated = true; vm.UserName = identity.Claims.FirstOrDefault(c => c.Type == "UserName").Value; vm.PhotoUrl