无法在asp.net core 2.0 Web App中实现Feature文件夹

时间:2018-05-07 07:46:38

标签: c# asp.net-mvc-4 .net-core

我正在尝试使用OdeToCode.AddFeatureFolders包在Core 2.0 Web应用程序中实现Feature文件夹。 屏幕截图和代码片段如下:

enter image description here

enter image description here

enter image description here

以下是我用于实现功能文件夹功能的两个类:

namespace FeatureWebApp.Infrastructure.FeatureFolders
   {
    public class FeatureViewLocationExpander : IViewLocationExpander
     {
    public void PopulateValues(ViewLocationExpanderContext context)
    {

    }

    public IEnumerable<string> ExpandViewLocations(
        ViewLocationExpanderContext context,
        IEnumerable<string> viewLocations)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }
        if (viewLocations == null)
        {
            throw new ArgumentNullException(nameof(viewLocations));
        }

        // {0} - Action Name
        // {1} - Controller Name
        // {2} - Area name

        //Features
        yield return "/Features/{1}/{0}.cshtml";

        yield return "/Features/{2}/{0}.cshtml";

        yield return "/Features/{1}/{2}/{0}.cshtml";

        yield return "/Features/{1}/Views/{0}.cshtml";

        //Feature Areas
        yield return "/Features/{2}/{1}/{0}.cshtml";
        yield return "/Features/{2}/{1}/Views/{0}.cshtml";
        yield return "/Features/{2}/Shared/{0}.cshtml";

        //Shared
        yield return "/Features/Shared/{0}.cshtml";

    }
 }
 }


   namespace FeatureWebApp.Infrastructure.FeatureFolders
   {
    public static  class ServiceCollectionExtensions
    {
     public static IMvcBuilder AddFeatureFolders(this IMvcBuilder services)
    {
        if (services == null)
        {
            throw new ArgumentNullException(nameof(services));
        }

        services.AddRazorOptions(o =>
        {
            o.ViewLocationExpanders.Add(new FeatureViewLocationExpander());
        });

        return services;
    }
 }
}

启动.cs: -

 public void ConfigureServices(IServiceCollection services)
       {

        services.AddMvc().AddFeatureFolders();
       }

这里的问题是除非我在creditcardapi控制器中写[Route(“CreditCardAPI”)]我无法使用“http:localhost:00000 / CreditCardAPI”直接调用CreditCardAPI方法。它显示我的错误 “没有找到网址的网页:http://localhost:61452/CreditCardAPI

我已在CreditCardAPI控制器上面写了[Area(“CreditCards”)],如上图所示。如果在家庭控制器中我还没有声明任何区域或任何路线,我可以直接调用家庭控制器方法(默认索引方法)而不需要蚂蚁区域或路由声明。

enter image description here

此外,如果我使用嵌套文件夹结构,如 - &gt;功能 - &gt; TroubleTickets - &gt; List-&gt;控制器,视图,与List相关的模型,如上图所示,在这种情况下如何调用CreditCardAPI控制器方法?

有任何建议或想法吗?我在这里缺少任何步骤?

1 个答案:

答案 0 :(得分:1)

OdeToCode.AddFeatureFolders软件包具有一些有关控制器名称空间的约定。

请参阅示例应用程序。 https://github.com/OdeToCode/AddFeatureFolders/blob/master/sample/WebApplication/Features/Bar/HomeController.cs

AddFeatureFolders uses the namespace of the controller to figure out where the views are.

您遇到的错误并不表示缺少视图,但可能仍与控制器的名称空间有关。请显示完整的控制器代码