Как узнать, откуда ViewComponent вызывается в ядре asp.net - PullRequest
0 голосов
/ 22 июня 2019

Допустим, у меня есть MenuViewComponent

public class MenuViewComponent : ViewComponent
{

    public MenuViewComponent()
    {
    }

    public async Task<IViewComponentResult> InvokeAsync()
    {
        return  View();
    }
}

И это вызывается из нескольких видов бритвы

например в

1.TopMenu.cshtml -> @await Component.InvokeAsync("Menu")
2.SideMenu.cshtml -> @await Component.InvokeAsync("Menu")

Теперь проблема в том, что я не могу изменить код компонента, чтобы добавить дополнительную информацию. Есть ли способ узнать, вызван ли компонент из TopMenu.cshtml или SideMenu.cshtml изменив код внутри

~/View/shared/Components/Menu/Default.cshtml

1 Ответ

1 голос
/ 22 июня 2019

Ваш класс наследуется от класса ViewComponent.Внутри класса ViewComponent имеется много методов.

Путь к представлению можно получить из ViewContext.

ViewContext.View.Path

Это свойство возвращает значение, подобное этому

"/Views/Home/Index.cshtml"

ViewComponent.cs включает в себя

public abstract class ViewComponent
    {
        protected ViewComponent();

        //
        // Summary:
        //     Gets the System.Security.Claims.ClaimsPrincipal for the current user.
        public ClaimsPrincipal UserClaimsPrincipal { get; }
        //
        // Summary:
        //     Gets the Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.
        public ViewDataDictionary ViewData { get; }
        //
        // Summary:
        //     Gets the Microsoft.AspNetCore.Mvc.ViewComponent.ViewContext.
        public ViewContext ViewContext { get; }
        [ViewComponentContext]
        public ViewComponentContext ViewComponentContext { get; set; }
        //
        // Summary:
        //     Gets or sets the Microsoft.AspNetCore.Mvc.IUrlHelper.
        public IUrlHelper Url { get; set; }
        //
        // Summary:
        //     Gets the Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.
        public ModelStateDictionary ModelState { get; }
        //
        // Summary:
        //     Gets the view bag.
        [Dynamic]
        public dynamic ViewBag { get; }
        //
        // Summary:
        //     Gets the Microsoft.AspNetCore.Mvc.ViewComponent.RouteData for the current request.
        public RouteData RouteData { get; }
        //
        // Summary:
        //     Gets or sets the Microsoft.AspNetCore.Mvc.ViewEngines.ICompositeViewEngine.
        public ICompositeViewEngine ViewEngine { get; set; }
        //
        // Summary:
        //     Gets the System.Security.Principal.IPrincipal for the current user.
        public IPrincipal User { get; }
        //
        // Summary:
        //     Gets the Microsoft.AspNetCore.Http.HttpRequest.
        public HttpRequest Request { get; }
        //
        // Summary:
        //     Gets the Microsoft.AspNetCore.Http.HttpContext.
        public HttpContext HttpContext { get; }
        //
        // Summary:
        //     Gets the Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionary.
        public ITempDataDictionary TempData { get; }

        //
        // Summary:
        //     Returns a result which will render HTML encoded text.
        //
        // Parameters:
        //   content:
        //     The content, will be HTML encoded before output.
        //
        // Returns:
        //     A Microsoft.AspNetCore.Mvc.ViewComponents.ContentViewComponentResult.
        public ContentViewComponentResult Content(string content);
        //
        // Summary:
        //     Returns a result which will render the partial view with name viewName.
        //
        // Parameters:
        //   viewName:
        //     The name of the partial view to render.
        //
        //   model:
        //     The model object for the view.
        //
        // Returns:
        //     A Microsoft.AspNetCore.Mvc.ViewComponents.ViewViewComponentResult.
        public ViewViewComponentResult View<TModel>(string viewName, TModel model);
        //
        // Summary:
        //     Returns a result which will render the partial view with name "Default".
        //
        // Parameters:
        //   model:
        //     The model object for the view.
        //
        // Returns:
        //     A Microsoft.AspNetCore.Mvc.ViewComponents.ViewViewComponentResult.
        public ViewViewComponentResult View<TModel>(TModel model);
        //
        // Summary:
        //     Returns a result which will render the partial view with name viewName.
        //
        // Parameters:
        //   viewName:
        //     The name of the partial view to render.
        //
        // Returns:
        //     A Microsoft.AspNetCore.Mvc.ViewComponents.ViewViewComponentResult.
        public ViewViewComponentResult View(string viewName);
        //
        // Summary:
        //     Returns a result which will render the partial view with name "Default".
        //
        // Returns:
        //     A Microsoft.AspNetCore.Mvc.ViewComponents.ViewViewComponentResult.
        public ViewViewComponentResult View();
    }

Но я предпочитаю, чтобы вы отправили параметр в свой Viewcomponent.Как перечисление.(Menu.TopMenu и Menu.SideMenu)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...