更改Asp.Net 5中的组件视图位置 [英] Change component view location in Asp.Net 5

查看:130
本文介绍了更改Asp.Net 5中的组件视图位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET 5上,组件"视图必须位于以下两个位置之一:

On ASP.NET 5 a Component view must be in one of two places:

Views/NameOfControllerUsingComponent/Components/ComponentName/Default.cshtml
Views/Shared/Components/ComponentName/Default.cshtml

是否可以将其更改为:

Views/NameOfControllerUsingComponent/Components/ComponentName.cshtml
Views/Shared/Components/ComponentName.cshtml

因此,基本上,删除文件夹ComponentName并将视图名称从Default.cshtml更改为ComponentName.cshtml.

So basically, remove the folder ComponentName and change the view name from Default.cshtml to ComponentName.cshtml.

对我来说更有意义...有可能吗?

For me it makes more sense ... Is it possible?

推荐答案

仅当您创建从基本

That convention is only applied if you create a view component that derives from the base ViewComponent provided by the framework.

该类定义View助手,该助手返回

That class defines the View helpers, which return a ViewViewComponentResult:

public ViewViewComponentResult View<TModel>(string viewName, TModel model)
{
    var viewData = new ViewDataDictionary<TModel>(ViewData, model);
    return new ViewViewComponentResult
    {
        ViewEngine = ViewEngine,
        ViewName = viewName,
        ViewData = viewData
    };
}

ViewViewComponentResult是定义约定的地方:

private const string ViewPathFormat = "Components/{0}/{1}";
private const string DefaultViewName = "Default";

public async Task ExecuteAsync(ViewComponentContext context)
{
    ...

    string qualifiedViewName;
    if (!isNullOrEmptyViewName &&
        (ViewName[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.
        var viewName = isNullOrEmptyViewName ? DefaultViewName : ViewName;

        qualifiedViewName = string.Format(
            CultureInfo.InvariantCulture,
            ViewPathFormat,
            context.ViewComponentDescriptor.ShortName,
            viewName);
    }

    ...

}

请注意,如果您从视图组件返回视图的完整路径作为视图名称,则视图组件将使用指定的视图. 像这样:

Notice that if you return from your view component the full path to a view as the view name, then the view component will use the specified view. Something like:

return View("~/Views/Shared/Components/ComponentName.cshtml")

由于无法修改ViewViewComponentResult中的约定,并且您的方法仅适用于具有单个视图的视图组件,因此可以使用根视图路径方法构建一些东西:

Since there is no way to modify the conventions in ViewViewComponentResult and your approach would only work for view components with a single view, you could build something using the root view paths approach:

  • 创建您自己的ViewComponent类,以扩展现有类.
  • 添加新的帮助器方法或隐藏现有的View方法以使用完整路径返回视图:

  • Create your own ViewComponent class extending the existing one.
  • Add new helper methods or hide the existing View methods to return a view using a full path:

public ViewViewComponentResult MyView<TModel>(TModel model)
{
    var viewName = string.Format(
            "~/Views/Shared/Components/{0}.cshtml", 
            this.ViewComponentContext.ViewComponentDescriptor.ShortName)
    return View(viewName, model);
}

  • 如果添加新方法,则可以将它们添加为ViewComponent的扩展方法,而不必创建自己的类.
  • If you add new methods you might be able to add them as extension methods of ViewComponent instead of having to create your own class.
  • 另一种替代方法是创建类SingleViewViewComponent,以复制

    Another alternative would be creating a class SingleViewViewComponent copying the code for ViewComponent but replacing the implementation of ViewViewComponentResult View<TModel>(string viewName, TModel model). Then when creating your view components, you would inherit from SingleViewViewComponent instead of ViewComponent.

    这篇关于更改Asp.Net 5中的组件视图位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆