Disable Caching For A Single JS File Using Web.config - c#

I currently cache everything possible on my site (images, JS, CSS). There is only one JS file that I need to be loaded fresh every single time. How do I omit just one file from caching, using web.config, whilst leaving everything else cached?
Note that I tried another link here, and it didn't seem to stop the caching of my file:
How do I disable caching of an individual file in IIS 7 using weserver config settings

How about:
<configuration>
<location path="path/to/the/file.js">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="DisableCache" />
</staticContent>
</system.webServer>
</location>
</configuration>
(Note that the path is relative to the web.config file)

I don't think you can do it using web.config, but you could add a unique querystring parameter to the javascript url in order for it to be loaded every time:
If you are using ASP.NET
<script src="mycode.js?<%=System.Guid.NewGuid.ToString()%>"></script>

Set the path for it not as a static URL but get an ASPX page to serve the script. Inside your ASPX page just send back the text:
byte[] javascriptTextBuffer = GetMyJavascript();
Response.ContentType = "text/javascript";
Response.Write(javascriptTextBuffer);
Inside the page turn off caching.
Having said that, it seems to me that you are doing something wrong that have to load the JavaScript file everytime. Make scripts static but use parameters to drive versatility.

Related

How to I make global.asax run when the URL has a dot?

I have a website running on IIS 7.5 which I can only access using FTP. (So I won't be able to use IIS Manager.)
I want to reorganise the URL structure, so I've modified the Global.asax code to look for the old URLs and Response.Redirect them to their new location. This works great for some of the URLs but not all.
With some experimentation, I found that when the old URL has a dot (eg, http://example.com/hello.html) then my global.asax code doesn't run. If I remove the file extension part of the URL, the global.asax code runs fine.
Those URLs with .html etc on the end are already out there and I can't change them now.
What do I need to do please?
What's happening is that IIS thinks your request for "/hello.html" is a static file and tries to serve it up directly without handing it off to your ASP.ENT application (before it gets to the routing in your Globals.asax file).
To change this behavior, you need to tell IIS to send these requests to your ASP.NET app instead of handling them itself. You can do this in your web.config file:
<system.webServer>
<handlers>
<add name = "LetMeHandleMyOwnStaticContent" path ="/*" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersion4.0"/>
</handlers>
</system.webServer>
Note, this will make you handle ALL of the static files (be prepared to handle the .js files, .css files, .html files, .png files, etc.) through your app. You can manipulate the path and verb to narrow down what you want to serve through your app. You can add multiple filters by repeating the "add" element (just use a unique name for each path).

ASP.NET Random string showing up in URL

I am developing website using Asp.Net 4.5 (VS2012) and using Telerik Ajax controls. I am using Telerik Schedular, Grid, AjaxPanel etc.
Project compiles fine. When I run it from VS.. It is showing me URL like this
http://localhost:23482/default.aspx
and then suddenly some random string inserted between URL. and it looks little wired like below
"http://localhost:23482/(S(hchi1ir5xii2dy1cjgpghqx3))/default.aspx"
"http://localhost:23482/(S(mgxsfl3rnxnbx2y24i12nowe))/default.aspx"
"http://localhost:23482/(S(sc2hyowh0f2xidnx1zptgaqd))/default.aspx"
I've also tried to run it from IIS as website. no luck.. same problem.
NOTE: The website still works fine even with random strings in URL nothing is broken at all.
Can anyone please suggest me something ?|
Thanks
Seems like you are using cookieless session
See this
SessionId by default is store in cookies but you can use cookieless session by writting this lines to web.config
<system.web>
<sessionState cookieless="true"
/>
</system.web>
you can remove that lines from webconfig to remove (S(hchi1ir5xii2dy1cjgpghqx3))
from
"http://localhost:23482/(S(hchi1ir5xii2dy1cjgpghqx3))/default.aspx"
or write to web.config
<system.web>
<sessionState cookieless="false"
/>
</system.web>

Getting broken images on localhost but not on websites

On our website we have images dropped on a grid with the following line:
imgUpdated.ImageUrl = "./images/Test_Icon.gif";
This works fine when published to production or test websites but within the IDE it gives broken (Red-X) images. Properties on the broken image says this:
http://localhost:52168/OurApp/images/Test_Icon.gif
If I attempt to past that into a browser, it redirects me to the login page with the following URL:
http://localhost:52168/OurApp/login.aspx?ReturnUrl=%2fITRequest%2fimages%2fTest_Icon.gif
I'd appreciate any help you might offer. I've already tried setting up a virtual directory in IIS and taking out the period but that didn't help.
create a web.config file with the following xml and place it in your images folder
<configuration>
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</configuration>
This is likely the reason your IDE doesn't show the images. If your browser is giving a 401 response and redirecting to your login page, the IDE is expecting an image MINE type.
You can find more information on the authorization section
Your image or any other content paths should start with forward slash / which points always to the root of your website. That way no matter in what environment you are your path will always be the same. So, answering your question... your path should look like this:
imgUpdated.ImageUrl = "/images/Test_Icon.gif";

asp.net hidden redirect with direction url

How to do it:
When you access a certain page (like a folder), you can view information specific user, for example:
www.page.com/user001
Opens the default page www.page.com/userinfo.aspx in which that user get "user001" and display certain information. And the user see www.page.com/user001
I can do this with asp.net or IIS7?
something like subdomains
You can do this by adding the URL Rewrite Module to IIS. Check this out
http://www.iis.net/learn/extensions/url-rewrite-module/using-rewrite-maps-in-url-rewrite-module
You can store the URL Rewrite rules in the web.config file. For example:
<configuration>
<system.webServer>
<rewrite>
<rules>
****(Your URL Rewrite Rules)****
</rules>
</rewrite>
</system.webServer>
</configuration>
But if it's not possible for you to store it in web.config due to security or maybe for some performance issues, then you can store the URL Rewrite Rules in IIS.
I hope it helps.
you can use routing in global.asax file at application_start event instead of rewrite the url also this will redirect every this to the profile page

How To Generate A Javascript File From The Server

I'm using BlogEngine.NET (a fine, fine tool) and I was playing with the TinyMCE editor and noticed that there's a place for me to create a list of external links, but it has to be a javascript file:
external_link_list_url : "example_link_list.js"
this is great, of course, but the list of links I want to use needs to be generated dynamically from the database. This means that I need to create this JS file from the server on page load. Does anyone know of a way to do this? Ideally, I'd like to just overwrite this file each time the editor is accessed.
Thanks!
I would create an HTTPHandler that responds with the desired data read from the db. Just associate the HTTPHandler with the particular filename 'example_link_list.js' in your web-config. Make sure you set
context.Response.ContentType = "text/javascript";
then just context.Response.Write(); your list of external links
if your 3rd party code doesn't require that the javascript file has the .js extension, then you can create your HTTPHandler and map it to either .axd or .ashx extension in web.config only - no need to change IIS settings as these extensions are automatically configured by IIS to be handled by asp.net.
<system.web>
<httpHandlers>
<add verb="*" path="example_link_list.axd" type= "MyProject.MyTinyMCE, MyAssembly" />
</httpHandlers>
</system.web>
This instructs IIS to pass all requests for 'example_link_list.axd' (via POST and GET) to the ProcessRequest method of MyProject.MyTinyMCE class in MyAssembly assembly (the name of your .dll)
You could alternatively use Visual Studio's 'Generic Handler' template instead - this will create an .ashx file and code-behind class for you. No need to edit web.config either.
using an HTTPHandler is preferrable to using an .aspx page as .aspx requests have a lot more overheads associated (all of the page events etc.)
If you can't change the file extension (and just return plain text, the caller shouldn't care about the file extension, js is plain text) then you can set up a handler on IIS (assuming it's IIS) to handle javascript files.
See this link - http://msdn.microsoft.com/en-us/library/bb515343.aspx - for how to setup IIS 6 within windows to handle any file extension. Then setup a HttpHandler to receive requests for .js (Just google httphandler and see any number of good tutorials like this one: http://www.devx.com/dotnet/Article/6962/0/page/3 )
Just point it at an aspx file and have that file spit out whatever javascript you need. I did this recently with TinyMCE in PHP and it worked like a charm.
external_link_list_url : "example_link_list.aspx"
In your aspx file:
<%# Page Language="C#" AutoEventWireup="false" CodeFile="Default.aspx.cs" Inherits="Default" %>
in your code-behind (C#):
using System;
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("var tinyMCELinkList = new Array(");
// put all of your links here in the right format..
Response.Write(string.Format("['{0}', '{1}']", "name", "url"));
Response.Write(");");
}
}

Categories

Resources