Reputation: 4528
I want to adapt the feature folders/slices for my aspnet core MVC app.
I have managed to server controller and cshtml files from the features folder.
With this code...
using Microsoft.AspNetCore.Mvc.Razor;
using System.Collections.Generic;
namespace MyApp.Web
{
public class FeatureFolderViewLocationExpander : IViewLocationExpander
{
public void PopulateValues(ViewLocationExpanderContext context)
{
/* no-op */
}
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
// {0} - Action Name
// {1} - Controller Name
// {2} - Area name
object area;
if (context.ActionContext.RouteData.Values.TryGetValue("area", out area))
{
yield return "/Areas/{2}/{1}/{0}.cshtml";
yield return "/Areas/{2}/Shared/{0}.cshtml";
yield return "/Areas/Shared/{0}.cshtml";
}
else
{
yield return "/Features/{1}/{0}.cshtml";
yield return "/Features/Shared/{0}.cshtml";
}
}
}
}
and..
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationExpanders.Add(new FeatureFolderViewLocationExpander());
});
}
... but I have not managed to server the .js files from the features folder (eg. home.js in my solution above).
Is is possible?
Upvotes: 1
Views: 302
Reputation: 12695
If you want to access the static files outside of web root , you could configure the Static File Middleware as follows :
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseStaticFiles();//For the wwwroot folder
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "Features")),
RequestPath = "/Features"
});
...
}
Then access it with the below path
<script src="~/Features/Home/home.js"></script>
Reference: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-3.1
Upvotes: 2