First I define a bundle:
var bootstrapBundle = new Bundle("~/bundles/css/styles").Include(
"~/assets/css/bootstrap.css");
And then, in my layout file:
#Styles.Render("~/bundles/css/styles")
which renders to:
<link href="/assets/css/bootstrap.css" rel="stylesheet" />
My problem is that I'm sharing my assets between 2 projects and I need the following:
<link href="assets/css/bootstrap.css" rel="stylesheet" />
(note the absense of leading spash in the stylesheet path)
Any way I can achieve that while still referencing the bundle? Thank you in advance.
I dont think it is a good idea to share the assests in development, but in production however there are buildin features to use so that you can share the known assets like bootstrap.css.
example:
bundles.UseCdn = true;
bundles.Add(new ScriptBundle("~/bundles/jquery", "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js").Include(
"~/Scripts/jquery-{version}.js"));
to test it make sure debug="false" in web.config.
source
http://www.dotnetjalps.com/2014/07/cdn-in-aspnet-mvc-bundling.html
Related
What is the explanation for a Blazor reference starting with a tilde? "~/(...)"
This can be found in _Hosts.cshtml file:
<link href="~/css/Chart.css" rel="stylesheet" />
<link href="~/css/site.css" rel="stylesheet" />
What is the exact meaning of "~"? What is the difference to just "/" or without?
Is it important to (not) use "~" if you refer to an external library?
I could not find an explanation for this.
In Razor .cshtml files, tilde-slash (~/) points to the web root.
The default web root is wwwroot.
A path beginning with ~/ is referred to as a virtual path.
The Tilde Slash is a feature of Razor, which converts a relative path to an absolute. Thus, the path for the href in <link href="~/css/Chart.css" rel="stylesheet" /> will be converted to an absolute path like this:
<link href="/css/Chart.css" rel="stylesheet" />
Note that the _Hosts.cshtml file contains the the base element like this:
<base href="~/" />
The "~/" is a relative path, and it is converted to <base href="/" />
The base URL is used to resolve relative URLs when the NavigationManager needs to obtain an absolute URL.
Is it important to (not) use "~" if you refer to an external library?
If by external you mean external to the Blazor App space, I guess you shouldn't use the tilde-slash feature at all. You should provide an absolute url, as for instance:
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
Note: As far as I know, the only place the tilde-slash is used in the context of Blazor is with the base element in the _Host.cshtml file
Hope this helps...
What is the difference to just "/" or without?
Imagine your app lived in a folder, and you d onto know the name or it differs between installs.
/dev/
/prod/
YOu can not use / and you do not want to know the name at compile time.
ASP.NET MVC (NOT (!) Blazor, that happens earlier) puts in the root origin of the application folder as seen in the request and determiend at runtime.
I'm trying to port THIS pasge and its HTML5 animation to a winform.
What I did is copied and added the index.html and all the css and js file to my project. This is how my project directory looks like
And then I'm trying to load the html file into my webbrowser object using this code snippet
private void Form1_Load(object sender, EventArgs e)
{
string curDir = Directory.GetCurrentDirectory();
this.webBrowser2.Url = new Uri(String.Format("file:///{0}/index.html", curDir));
}
And I have set properties of all the file to Copy Always
Now when I run the project I get a script error message and upon pressing Yes
it loads the page but only background is displayed and the animation is lost!
please help!
PS: I've changed
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script src="js/ThreeWebGL.js"></script>
<script src="js/ThreeExtras.js"></script>
To
<link href="main.css" rel="stylesheet" type="text/css" />
<script src="ThreeWebGL.js"></script>
<script src="ThreeExtras.js"></script>
in index.html file
Screenshots of two script error that I'm getting--
It works in IE10
The *.js files need to be in a js folder. Either create the folder and move them or edit the html file.
EDIT1:
Does it work if you point it to "http://www.script-tutorials.com/demos/177/index.html"?
EDIT2:
Add:
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
in the html head section.
EDIT3:
I tried on my machine and it's the same behavior. But the page does run fine in Internet Explorer 11. Maybe the WinForms control uses IE engine in some compatibility mode.
I would use http://www.awesomium.com/
The webbrowser control is just an instance of IE - that page doesn't work for me in Internet Explorer so it's unlikely to work in the embedded browser. Search for webkit net - might give you better results. I'm tempted to ask why you want to do this but I'm sure you've got a good reason.
Got it working! Basically the set up works fine. Just had to open main.css file and comment out all the footer properties to get rid of the footer and found another script ref which was pointed to js/script.js had to change it to script.js
Now how can i increase the framerate so that it doesnt stutter? Theres a lag at the moment
In a .NET MVC4 project how does #Styles.Render works?
I mean, in #Styles.Render("~/Content/css") which file is it calling?
I dont have a file or a folder called "css" inside my Content folder.
It's calling the files included in that particular bundle which is declared inside the BundleConfig class in the App_Start folder.
In that particular case The call to #Styles.Render("~/Content/css") is calling "~/Content/site.css".
bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
Watch out for case sensitivity. If you have a file
/Content/bootstrap.css
and you redirect in your Bundle.config to
.Include("~/Content/Bootstrap.css")
it will not load the css.
A bit late to the party. But it seems like no one has mentioned
bundling & minification of StyleBundle, so..
#Styles.Render("~/Content/css")
calls in Application_Start():
BundleConfig.RegisterBundles(BundleTable.Bundles);
which in turn calls
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/Site.css"));
}
RegisterBundles() effectively combines & minifies bootstrap.css & Site.css
into a single file,
<link href="/Content/css?v=omEnf6XKhDfHpwdllcEwzSIFQajQQLOQweh_aX9VVWY1" rel="stylesheet">
But..
<system.web>
<compilation debug="false" targetFramework="4.6.1" />
</system.web>
only when debug is set to false in Web.config.
Otherwise bootstrap.css & Site.css will be served individually.
Not bundled, nor minified:
<link href="/Content/bootstrap.css" rel="stylesheet">
<link href="/Content/Site.css" rel="stylesheet">
src="#url.content("~/Folderpath/*.css")" should render styles
As defined in App_start.BundleConfig, it's just calling
bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
Nothing happens even if you remove that section.
Polo I wouldn't use Bundles in MVC for multiple reasons. It doesn't work in your case because you have to set up a custom BundleConfig class in your Apps_Start folder. This makes no sense when you can simple add a style in the head of your html like so:
<link rel="stylesheet" href="~/Content/bootstrap.css" />
<link rel="stylesheet" href="~/Content/bootstrap.theme.css" />
You can also add these to a Layout.cshtml or partial class that's called from all your views and dropped into each page. If your styles change, you can easily change the name and path without having to recompile.
Adding hard-coded links to CSS in a class breaks with the whole purpose of separation of the UI and design from the application model, as well. You also don't want hard coded style sheet paths managed in c# because you can no longer build "skins" or separate style models for say different devices, themes, etc. like so:
<link rel="stylesheet" href="~/UI/Skins/skin1/base.css" />
<link rel="stylesheet" href="~/UI/Skins/skin2/base.css" />
Using this system and Razor you can now switch out the Skin Path from a database or user setting and change the whole design of your website by just changing the path dynamically.
The whole purpose of CSS 15 years ago was to develop both user-controlled and application-controlled style sheet "skins" for sites so you could switch out the UI look and feel separate from the application and repurpose the content independent of the data structure.....for example a printable version, mobile, audio version, raw xml, etc.
By moving back now to this "old-fashioned", hard-coded path system using C# classes, rigid styles like Bootstrap, and merging the themes of sites with application code, we have gone backwards again to how websites were built in 1998.
I did all things necessary to add bundling to an MVC 3 web (I'm new to the existing solution). Styles.Render didn't work for me. I finally discovered I was simply missing a colon. In a master page: <%: Styles.Render("~/Content/Css") %> I'm still confused about why (on the same page) <% Html.RenderPartial("LogOnUserControl"); %> works without the colon.
Set this to False on your web.config
<compilation debug="false" targetFramework="4.6.1" />
Simple enough question: I've created a small app that is basically just a favourites that sits in my system tray so that I can open often-used sites/folders/files from the same place. Getting the default icons from my system for known file types isn't terribly complicated, but I don't know how to get the favicon from a website. (SO has the grey->orange stack icon in the address bar for instance)
Does anyone know how I might go about that?
You'll want to tackle this a few ways:
Look for the favicon.ico at the root of the domain
www.domain.com/favicon.ico
Look for a <link> tag with the rel="shortcut icon" attribute
<link rel="shortcut icon" href="/favicon.ico" />
Look for a <link> tag with the rel="icon" attribute
<link rel="icon" href="/favicon.png" />
The latter two will usually yield a higher quality image.
Just to cover all of the bases, there are device specific icon files that might yield higher quality images since these devices usually have larger icons on the device than a browser would need:
<link rel="apple-touch-icon" href="images/touch.png" />
<link rel="apple-touch-icon-precomposed" href="images/touch.png" />
And to download the icon without caring what the icon is you can use a utility like http://www.google.com/s2/favicons which will do all of the heavy lifting:
var client = new System.Net.WebClient();
client.DownloadFile(
#"http://www.google.com/s2/favicons?domain=stackoverflow.com",
"stackoverflow.com.ico");
Updated 2020
Here are three services you can use in 2020 onwards
<img height="16" width="16" src='https://icons.duckduckgo.com/ip3/www.google.com.ico' />
<img height="16" width="16" src='http://www.google.com/s2/favicons?domain=www.google.com' />
<img height="16" width="16" src='https://api.statvoo.com/favicon/?url=google.com' />
You can use Google S2 Converter.
http://www.google.com/s2/favicons?domain=google.com
Source: http://www.labnol.org/internet/get-favicon-image-of-websites-with-google/4404/
This question is the first google search result I got when I keep searching for website favicon API. So I think it'll be still helpful in the future.
https://icon.horse/icon/[url.hostname] will give you a better site icon.
https://icon.horse/icon/stackoverflow.com
You can do it without programming in 3 steps:
1. Just open the web site, right-click and select "view source" to open the HTML code of that site. Then in the text editor search for "favicon" - it will direct you to something looking like
<link rel="icon" href='/SOMERELATIVEPATH/favicon.ico' type="image/x-icon" />
Take the string in href and append it to the web site's base URL (let's assume it is "http://WEBSITE/"), so it looks like
http://WEBSITE/SOMERELATIVEPATH/favicon.ico
which is the absolute path to the favicon. If you didn't find it this way, it can be as well in the root in which case the URL is http://WEBSITE/favicon.ico.
2. Take the URL you determined and insert it into the href-Parameter of the following code:
<html>
<head>
<title>Capture Favicon</title>
</head>
<body>
<a href='http://WEBSITE/SOMERELATIVEPATH/favicon.ico' alt="Favicon"/>Favicon</a>
</body>
</html>
3. Save this HTML code locally (e.g. on your desktop) as GetFavicon.html and then double-click on it to open it. It will display only a link named Favicon. Right-click on this link and select "Save target as..." to save the Favicon on your local PC - and you're done!
It's a good practice to minimize the number of requests each page needs.
So if you need several icons, yandex can do a sprite of favicons in one query.
Here is an example
http://favicon.yandex.net/favicon/google.com/stackoverflow.com/yandex.net/
The first thing to look for is /favicon.ico in the site root; something like WebClient.DownloadFile() should do fine. However, you can also set the icon in metadata - for SO this is:
<link rel="shortcut icon"
href="http://sstatic.net/stackoverflow/img/favicon.ico">
and note that alternative icons might be available; the "touch" one tends to be bigger and higher res, for example:
<link rel="apple-touch-icon"
href="http://sstatic.net/stackoverflow/img/apple-touch-icon.png">
so you would parse that in either the HTML Agility Pack or XmlDocument (if xhtml) and use WebClient.DownloadFile()
Here's some code I've used to obtain this via the agility pack:
var favicon = "/favicon.ico";
var el=root.SelectSingleNode("/html/head/link[#rel='shortcut icon' and #href]");
if (el != null) favicon = el.Attributes["href"].Value;
Note the icon is theirs, not yours.
In 2020, using duckduckgo.com's service from the CLI
curl -v https://icons.duckduckgo.com/ip2/<website>.ico > favicon.ico
Example
curl -v https://icons.duckduckgo.com/ip2/www.cdc.gov.ico > favicon.ico
You can get the favicon URL from the website's HTML.
Here is the favicon element:
<link rel="icon" type="image/png" href="/someimage.png" />
You should use a regular expression here. If no tag found, look for favicon.ico in the site root directory. If nothing found, the site does not have a favicon.
HttpWebRequest w = (HttpWebRequest)HttpWebRequest.Create("http://stackoverflow.com/favicon.ico");
w.AllowAutoRedirect = true;
HttpWebResponse r = (HttpWebResponse)w.GetResponse();
System.Drawing.Image ico;
using (Stream s = r.GetResponseStream())
{
ico = System.Drawing.Image.FromStream(s);
}
ico.Save("favicon.ico");
Sometimes we can't get the favicon image with the purposed solution as some websites use .png or other image extensions. Here is the working solution.
Open your website with a firefox browser.
Right-click on the website and click the "View page info" option from the list.
It will open up a dialog and click on the "Media" tab.
In that tab you will see all the images including favicon.
Select the favicon.ico image or click through the images to see which image is used as favicon. Some websites use .png images as well.
Then click on the "Save As" button and you should be good to go.
thanks!
This is a late answer, but for completeness: it is difficult to get even close to fetching 90% all favicons.
A while ago I wrote a WordPress plugin which attempts to get closer to 100%.
This is how it works:
It starts by searching existing favicon repositories such as Google favicons and GetFavicons for the favicon.
If none of them returns an icon, the plugin attempts to get the icon itself. This involves traversing several pages on the domain.
The plugin then inspects the physical image file, because on some servers files get returned with the incorrect mime types.
The code is still not perfect because in the details you will find many weird situations: people have wrongly coded paths, e.g. img/favicon.ico where img is not in the root, duplicate headers in HTML output, different server responses from the head and body etc.
The core of the fetching part is here so you can reverse-engineer it, but be aware that validating the response should be done (checking image filetype, mime etc.).
The SHGetFileInfo (Check pinvoke.net for the signature) lets you retrieve a small or large icon, just as if you were dealing with a file/folder/Shell item.
http://realfavicongenerator.net/favicon_checker?site=http://stackoverflow.com gives you favicon analysis stating which favicons are present in what size. You can process the page information to see which is the best quality favicon, and append it's filename to the URL to get it.
You can use Getfv.co :
To retrieve a favicon you can hotlink it at... http://g.etfv.co/[URL]
Example for this page : http://g.etfv.co/https://stackoverflow.com/questions/5119041/how-can-i-get-a-web-sites-favicon
Download content and let's go !
Edit :
Getfv.co and fvicon.com look dead. If you want I found a non free alternative : grabicon.com.
Using jquery
var favicon = $("link[rel='shortcut icon']").attr("href") ||
$("link[rel='icon']").attr("href") || "";
Style sheet in master page is not working for one web page of asp.net application but it works for another web page.
If you are referencing a css file from a master page you should ensure it has an absolute path, that way it will work everywhere. For example:
<head runat="server">
<link type="text/css" rel="stylesheet" href="~/_styles/mystylesheet.css" />
</head>
The important thing to note here is that the head tag has the runar="server" attribute and that i am specifying the full virtual path using a tilde ("~").
Are none of its style elements being included? Is it being over ridden( they are Cascading Style Sheets)? Does it have the correct CSS include statement?
Are your pages in different levels of folders ?
For example,
..\main.css
..\folder1\MasterPage.master
..\folder1\css_working.aspx
..\folder1\folder2\css_not_working.aspx
in this scenario you should define your css in masterpage as :
<link rel="stylesheet" type="text/css" href="../main.css" />
And take your pages to same level, like that :
..\main.css
..\folder1\MasterPage.master
..\folder1\css_working.aspx
..\folder2\css_not_working.aspx
If you are using update panels there are some cases where the styling may be lost for AJAX toolkit controls. To fix this you need to put hte full name of hte class items into the stylesheet instead of letting hte toolkit handle this.
Also be sure to use a relative url where possible so that if a file moves it won't loose it's mapping.
Use Firebug or Debug Bar, these tools will show you all the styles being employed on each element, so you can see what stylesheets it is using and which ones it is not.
Also, when you build check for any warnings about stylesheets that it can't reference etc.
it could be a permission issue on the folder... if you have deny users="?" in your web config.. make sure you have an allow users on the folder where you have your style sheets