I am new to C# and asp.net and would like to know the following:
Can I reference jquery library in the following format?
src="http://~/sites/booksite/tools/js/jquery.tools.min.js"
it does not give a compilation error or anything but want to make sure..
Thank you
The ~/ (tilde+slash) method of referencing paths is an ASP.NET thing - URLs on elements with runat="server" (i.e. server-side controls) will be evaluated and expanded from the relative path (where ~/ is the root of the application or virtual directory.) If the ASP.NET engine isn't doing this, then it doesn't get done.
In order to specify a relative path from the root, you should be able to get away with just the slash:
src="/sites/booksite/tools/js/jquery.tools.min.js"
Alternatively, apply the runat="server" value, and it would work:
runat="server" src="~/sites/booksite/tools/js/jquery.tools.min.js"
But when using the tilde+slash, then http:// won't work.
No. if the jquery is local to your site you can use the ~ to represent the root of your site. Provided that this src attribute is on a control with runat=server. But providing the http:// is unneeded in that case.
No you must definetly cannot. The "root folder" of your "application" is managed by the server, your application doesn't care if it's in / (development machine) or /prettyapp (production server).
What you can do however is ask ASP.NET figure out the path for you and fill it in:
<script src='<%= ResolveUrl("~/sites/booksite/tools/js/jquery.tools.min.js") %>'></script>
As a node, don't just use absolute paths (/something/) like Mr. Disappointment suggests, your application will die if you deploy it to a virtual directory.
This isn't the right way.
The Tilde will only be processed if the tag has runat="server", so yes you can do it but you shouldn't.
The ONLY reason you would want to specify a full http path to the script file is if you are hosting it via a content delivery network (CDN).
However, jQuery is already hosted on arguably the largest CDN -> Google. See http://code.google.com/apis/libraries/devguide.html
So, I would just leverage the resources that they give for free.
Related
I'd like to know how to resolve this problem about my asp.net webForms project. When I run the project and remove the filename in the address bar it shows all the files in my web project. Please help how to fix this. thanks ! I'm worried this exposed my codes and all stuff.
Even if you change the default page either by setting in web.config or using 'set as default page' option, directory listing will still work. you need to disable directory browsing when deploying your application.
Check this msdn page.
Right click on any aspx page you want to open and choose 'Set as default page'.
There is no default page in your application. Hence, your development server will show you the directory listing (I think IIS won't show this at all).
You might want to rename your WebForm1.aspx to Default.aspx, the name that is commonly used for the default page (and it is in the default rules), or you can change the default document by putting some rules in your web.config file.
Is it possible to add a url from another domain to the bundling in Microsoft.Web.Optimization?
I want to add a reference to replace the following link:
<link href='http://fonts.googleapis.com/css?family=PT+Sans+Narrow:400,700' rel='stylesheet' type='text/css'>
My code for creating a css bundle which works with local files is as below:
Bundle cssCommon = new Bundle("~/cssCommon", typeof(CssMinify));
cssCommon.AddDirectory("~/content/", "aom.common.*", false);
BundleTable.Bundles.Add(cssCommon);
Actually leaving that content on the other host is probably a better thing to do.
You benefit from the Google's global distribution potentially this means that the font may be 'closer' on the internet and have less lag than your actual web server.
You can download the content in parallel with your main content (each host is a separate set of downloads threads so it won't block your existing content or add to the total transfer time of that content)
You benefit from Google's server resilience and up time.
The end user real world experience may actually be better as a result.
That doesn't really make sense because bundling reduces the number of requests from the website's server. Referencing goodleapis is obviously on another server.
There is the concept of CDNs though, and these can be changed depending on the release, this link has some useful information on using a CDN with Bundling and Minification.
I make use of the bundling features in MVC4 by calling bundles.EnableDefaultBundles();, this allows me to browse to http://website.com/content/css which outputs a singular file of all the CSS files in the /content directory - great.
The issue is I have the following route which loads a blog post from the DB by title: /post/{anything} and in this case {anything} is css (for arguments sake, I can't change it) so the bundling is getting confused and trying to bundling everything in the post directory, which doesn't exist.
Is there any way to exclude a particular URL format or route from being bundled? I think not calling EnableDefaultBundles() would work but does that mean I would have to create bundles for everything manually?
Are you using an old version of the Optimization package? EnableDefaultBundles was removed prior to 1.0. You can still accomplish the equivalent of that method by adding the equivalent js/css DynamicFolderBundles.
I have a question about relative path to external javascript, css and images files in Asp.C# application.
I have pages with 2 - 5 sub levels so my javascript and css files looks like
../../../../../javascriptfile.js
../../../cssfile.css
../../../../../../image.jpg
In case if it will be additional sub level application won't find files.
What is the best practices to specify path to file???
Keep in mind when considering these answers that "root-relative" and "root of the site" may really mean the root of the path following the domain name in the url. You may need to take into account scenarios where your web site is not located at the root. In such scenarios, root-relative paths would potentially point to a different web site.
In ASP.NET you can use a leading ~ to generate urls relative to the root of current site for most server-side controls, as in:
<img src="~/image.jpg" runat="server">
You can also use the ResolveUrl method (and other similar methods) to expand such paths without using server-side controls.
Use a root-relative path like this:
/js/javascriptfile.js
/css/style.css
the first / means at the root of the site.
Use absolute or root-relative paths to avoid confusion in multi-level pages.
Look here for more information:
http://www.motive.co.nz/glossary/linking.php
I have seen on various websites how developers version their css/javascripts files by specifying querystrings similar to:
<head>
<link rel="stylesheet" href="css/style.css?v=1">
<script src="js/helper.js?v=1">
</head>
How is that done? Is it a good practice? I've been searching around but apparently, I'm not looking for the right terms. If it matters, I'm using ASP.NET.
Edit:: I just noticed (via Firebug) that if I "version" my files (?v=1) they will always be loading and will always override the cache. Is there a way around that?
Thanks in advance.
They're not really versioned. We do that because certain browsers won't always request the stylesheets properly (they won't even check for a last modified) so to force them to make a new request, you can bump the number in your html file that references it. It's kind of a hack, but it works.
This helps with caching when you want it to and forcing to download when you don't. Files are cached based on their path. So if the path is the same then it can pull from cache. But if they are different, hence a new version, then it would not use the cache but should pull the new file. At least that is how I have used this.
They are doing this to make caching for the Browsers more reliable. You can add the version manually, and increment it every time you change the file. This way the Browser thinks it's got a new file and downloads it for sure.
I don't know the way how to do this automatically in ASP.NET, Ruby on Rails for example checks the last changed timestamp on the file and adds this as version number to the file. I'm sure you'll be able to do something similar in ASP.NET.