DanAbdn DanAbdn - 1 year ago 116
ASP.NET (C#) Question

ASP.NET Core - environmental variables ignored for Layout in Area

I have a problem in my ASP.NET Core MVC application.

Can anyone advise why the tag helper for environment (names) isn't working for my (administration) layout page when located inside an Area?

When referencing my scripts using the below code, it works when placed into the Standard _Layout.cshtml page but not a _Layout.cshtml contained within an area (used for an administration area).
I am ideally trying to modularise my code into Areas - the dashboard contains this _Layout.cshtml page for the administration area.

I discovered that all my css/js references were being included (unminified and minified and CDN). So I put the below code into public layout and admin layout.

<environment names="Development">
<environment names="Staging,Production">
Staging production

~/Views/Shared/_Layout.cshtml (public)

"Development" is displayed (good)

~/Areas/Dashboard/Views/Shared/_Layout.cshtml (administration)

"Development Staging Production" is displayed (bad)

My environmental variable is
ASPNETCORE_ENVIRONMENT (with a value of Development)

My start up

public Startup(IHostingEnvironment env)
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)

if (env.IsDevelopment())
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709

Configuration = builder.Build();

Configure method

app.UseMvc(routes =>
routes.MapRoute(name: "areaRoute",
template: "{area:exists}/{controller=Home}/{action=Index}");

routes.MapRoute(name: "default",
template: "{controller=Home}/{action=Index}/{id?}");

Any help would be great - thank you.

Answer Source

Sounds like the tagHelper is not getting registered in the Area and is being processed by the browser.

TagHelpers like environment have to be referenced in your _ViewImports.cshtml file, which by default will just be in /Views but also should exist in each of your areas.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download