如何知道在ASP.NET Core中从何处调用ViewComponent

时间:2019-06-22 16:10:07

标签: asp.net-core razor asp.net-core-viewcomponent

比方说,我有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 个答案:

答案 0 :(得分:1)

您的类是从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)