Change an appsettings.json value on publish netCore6 - c#

Is it possible to increase a value in the appsettings.json when I publish a project?
I want to add some kind of number to all css and js files, because it seems they keep getting cached. I want a value in my appsettings.json (or somewhere), I'll add to all css/js includes.
When I publish the project, I would like this number to increase.
Is there a way to do this? Or is there some other way to force a refresh of css/js on the client-side when changes are made?

You can use the built-in asp-append-version if you're referencing your CSS and JS files directly. Like
<script src="your-file-here.js" asp-append-version="true"></script>
That will append a "version" to the file path (which is calculated from a hash of the file content, thus it will update whenever the file does). In my experience it works pretty well :-)
https://www.c-sharpcorner.com/blogs/aspappendversion-feature-in-asp-net-core

Related

asp-append='true' adding some random value to type in script tag in asp dotnet core 3.1

When am adding asp-append-version="true" then it's generating type with some random number in live server. In my local system it's working well, no such thing is generating.
Any help for this please
<script src="~/js/view/SignalrFunctions.js" asp-append-version="true"></script>
and output in live is
<script src="/js/view/SignalrFunctions.js?v=r6rspvdRJxE77iwZz4pxl9MBcr2z6gL1ckobKWF3rts" type="2707b0341fc8574517d57e9b-text/javascript"></script>
We normally use static files like CSS, Image and JS files to our website to make it interactive and dynamic. But when we update any file like css, js or image then we need to tell the browser to take the latest file, not the oldest one. But we really don't want user to do full refresh each and every time when we change/update css, js or image file. And asp-append-version="true" is to help us solve this problem.
So this additional attribute helps us to tell the browser to always use updated content whether it is for CSS, JS or any Image file. The random string that appears just tells the browser to use the updated file.

rebuild url with mvc bundle

On the website I'm building we are using the built in bundling and minification.
We render our scripts by using the #Scripts.Render
#Scripts.Render("~/bundles/scripts")
it generates a link like:
bundles/scripts?v=3-DUUAAegZl4yp1O4V0VL0GnJ0U6gT3De8yKb41lfGs1
Here I thought that the hash:
v=3-DUUAAegZl4yp1O4V0VL0GnJ0U6gT3De8yKb41lfGs1
would be smart and change if we made changes to the inlcuded scripts but it seems like that is not the case.
Now when we are trying to implement a custom cdn for out bundling it fails on updates since the hash never seems to change.
Does anyone know of a solution to change the hash when the script changes or if we are doing something wring?
Edit:
I noticed that there is a difference in behavior depending on what environment I'm using.
Local development it seems to be working but it doesn't work on the staging server.
Should there be a difference?
Edit2:
Seems to be working as it should on production servers as well. Will have to do more research on what the cause can be.
Edit3:
Seem that the one causing the problem was in the registry LogRewrittenUrlEnabled was set to false to allow url rewrites at the same time as using dynamic content compression (gzip) so will have to find another way to enable this
You're right, the cache-buster hash should change if you change the contents of a script. There's some useful information about how the cache for bundling works that may help you here ASP.NET MVC Bundling cache. (Detecting css files changes) (internal behaviour).
The problem was that on the server we had in the registry
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp\Rewrite /v LogRewrittenUrlEnabled set to false
And if you have that then the cache-buster hash is not refreshing when the content i bundle is changed.

Bundle problems

I can't seem to get the javascript files to update in VS2013, asp.net using bundles. The image is after I have done a clean/rebuild, bundle clear and resetall, and deleted the browser history in IE (including cookies, website data, temp files and website files.) I have wasted day after day trying to get my site working. I have googled this a hundred times and found no satisfactory answers. What am I missing? There has got to be an easy way to do this.
As I said in the comment, don't use bundles during development of javascript or CSS files at all.
Just comment out the part where the bundle is rendered and include the corresponding files the classic way like this:
<script src="~/Scripts/myscript.js?v=#DateTime.Now.Ticks" type="text/javascript"></script>
When deploying the project remove that line and reactivate the previously commented bundle-rendering call.
Because the uri-parameter version string of the "classic-approach" is changing on each request you can be sure that you always have the latest version of javascript file and are not dependent on some CacheDependency of the Bundle-Framework to trigger or update (which apparently is not working all of the time).
Just don't forget to remove that manual script include so that your users' browsers can actually cache the resources.

C# asp.net application, changes to Site.css do not seem to affect Site.Master

I just started an asp.net c# project and I was trying to change the text-align of the .title and background color of the .header. I add the required code to the style sheet, but nothing changes in the header of the form. I'm probably missing something simple here, can someone point me in the right direction?
Stylesheets are usually cached by the browser to speed up browsing.
You can either force-refresh (SHIFT+F5) or append a value to the stylesheet path to cache-bust it.
style.css?v1
By changing v1 whenever you want a change to be forced out to all browsers, it will replace the cached version as resources are cached per URI.
Just try stopping your Local development server and do clean and build solution before running the application.
Even clear your temporary internet folder which may contain stale copy of your css file.
Right clicking in the markup view , you should see an options that says view in browser. This should reload anything cached.

Dynamically setting CSS values using ASP.NET

I'm working on a site where the images and other resources will be located on a separate domain from the main content of the site. We will use something like 'www.example.com' for the main site, and then 'images.example.com' for all extra resources for styles, etc.
When developing the site I will keep all of these resources on local dev. machines. The challenge here is keeping CSS references consistent between the production server and development environments.
What I was thinking of doing was creating a web.config key that would store the URL of the images server. Then, when switching from development to production I could just change the web.config value and everything would be done.
Is there any way to add a value to a CSS file, dynamically or otherwise, from some place in a config or C# class? Or am I going about this the wrong way?
Also, I'm limited to using .NET 2.0 if that makes a difference.
UPDATE
To expand on this a little more, I know I can use a web.config setting for server controls' URLs. Those are already generated dynamically. What I'm more interested in is what options I have for modifying (or doing "something") to static CSS files that will allow me to change URLs for things such as background image resources that would be referenced in CSS. Is there anything I can do besides find/replacing the values using my IDE? Perhaps something that can be done automatically with a deployment script?
Is keeping the CSS file on the image server an option? If that it possible, you could make all the image references relative, and then you just need to update the link to the css file.
<link rel="stylesheet" href="<%= ConfigurationManager.AppSettings("css-server") %>style.css" />
If you still want to send or generate a css file dynamically:
css files don't have to end in css. aspx is fine. You could do this:
<link rel="stylesheet" href="style.aspx" />
and then in your style.aspx page:
protected void page_load(){
Response.ContentType = "text/css";
if (ConfigurationManager.AppSettings("css-server") == "local") {
Server.Transfer("css/local.css");
} else {
Server.Transfer("css/production.css");
}
}
If you still want to dynamically generate a css file, I'd use an HttpHandler, set the contenttype to "text/css", then generate the css with Response.Write. If you insist on having the page end in css, you could always register css to go to asp.net in IIS, then on incoming requests in global.asax application_Begin request, if the file ends in .css, use httpcontext.current.rewritepath to your handler.
This will have a net effect of style.css being dynamically generated at runtime.
What about putting a place holder on the web page and then selecting which CSS file to utilize (PROD, TEST, etc.) at run time and add it to the place hodler?
I think that Update had the right idea...
<link rel="stylesheet" href="<%= ConfigurationManager.AppSettings("css-server") %>style.css" />
Sounds like a job for a NAnt [link] script to me. They're pretty easy to work with and well documented.
That way your code has isn't changing your css links, they're being updated at deploy time. This isn't a code issue, it's a deployment issue, so addressing it as such feels more "right" to me. That way you know if it loads correctly (with the right images) the first time it will load every time. NAnt scripts are a good thing to have in your toolbox.
The other solutions will work, but that code will be running every time the page loads for a change that should have happened once -- when the app was deployed.
You duped your own question:
https://stackoverflow.com/questions/449236/dynamically-setting-css-values-using-asp-net
This is a common problem. What we do is have seperate web.config files for each environment. There is a appSettings key in the web.config and any config values go there like this.
<appSettings>
<add key="ImagePath" value="d:\websites\www.site.com\www\images\" />
<appSettings>
When setting the image control in the code behind, use the following:
myImage.ImageUrl = + _
System.Configuration.ConfigurationSettings.AppSettings("ImagePath") + "image1234567890.jpg"
Just change your ImagePath key to correspond with the path on the production or qa servers. Also, you could make the test server have the same path, but in my experience this solution works.
I would create a server control for my CSS that registered the css script block on page load. You could very easily change all paths at that point programmatically.
Perhaps you can do something with the hosts file on your dev server(s)? That way you won't have to actually change any code.
It IS possible to send files with the .css extension through the asp.net engine, though. You could also have .ashx handlers that return valid css and reference those handlers in the tags. Seems like kind of a waste of processor for stuff that is 90% static text though.

Categories

Resources