简体   繁体   中英

DotNetCore MVC Using asp-append-version="true" in an Area

I have a web application that makes use of a number of Areas to hold various sections.

Each area makes use of a unique "_layout.cshtml" view.

I have realised that when pages in these areas are rendered the asp-append-version="true" attribute usually added to the css and javascript link/script tags is not rendering the querystring at the end of the href/src.

I believe this is because these pages are not being served as static files - correct me if I'm wrong.

This is the code in the Configure part of my Startup class

app.UseStaticFiles(new StaticFileOptions
{
   OnPrepareResponse = ctx =>
   {
      const int durationInSeconds = 31536000;
      context.Context.Response.Headers[HeaderNames.CacheControl] = "must-revalidate,max-age=" + durationInSeconds;
    }
});

And I suspect that I need to add something here, but I don't know what?? How do I get the rendered pages in these areas to append the querystring in the same way it does in the main application?

Make sure you've enabled Taghelpers in your Area With a _ViewImports.cshtml Page:

@using your webapp name
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

When you create a new MVC app, it would be added to your main app by default,if you add an area,don't forgot to enable it manually.

It works well in my case now:

在此处输入图像描述 在此处输入图像描述

You could also enable the taghelper in page with

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

for more details,you could check this document

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM