Favicon Icon-MVC3 ASP.NET - asp.net

What is the need of favicon.ico. I am trying to use MVC error handling and it complaines file being missed.How do get rid off this error.
Thanks

Go to the RegisterRoutes method of the Global.asax.cs file, and add this, as one of the first lines:
routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?favicon.ico(/.*)?" });

I suggest you use it, instead of getting rid of it. Cause it's part of the web standards in UI design. Don't you like branding of your site? Then this is a crucial part of that branding.

Solution:
<link rel="SHORTCUT ICON" href="http://www.youraddress.com/youricon.ico"/>" if you use localhost then href="http://localhost:port/youricon.ico"
Note:Use other name instead favicon.ico.

Add the following to the beginning of your RegisterRoutes method:
routes.IgnoreRoute("favicon.ico");
Your browser will always ask the site for the favicon, this will ensure mvc doesn't try to resolve it.

Related

Spring Boot custom favicon.ico not showing

I know this question has been asked over and over here and there are several solutions. I've tried several of those except the ones that suggests writing you own configuration bean for this. I don't want to do all that just to display a tiny icon it seams overkill. But I can not get it to work. These are the solutions I've tried so far.
just add favicon.ico under static resources and it should work....it doesn't.
spring.mvc.favicon.enabled=false in application.properties, no favicon showed at all (which I guess is the whole point of that).
Tried 2 examples of including the favicon as a link in the html pages. Like so:
<link rel="icon" type="image/png" href="favicon.png" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
Neither of those work.
Tried renaming my own favicon to something else and reference it as above. Does not work.
When inspecting the page in the browser I sometimes get no error at all printed out despite no icon showing, or I get an error saying GET http://localhost:8080/myapp/favicon.png 404 () Where it is refering the type as JSON (which I find strange).
I'm running out of ideas here so if anyone can tell me why this is not working please let me know. Did I perhaps forget one of those magic spring annotations?
This is what my main class looks like.
#SpringBootApplication
#ComponentScan
#Configuration
#EnableWebMvc
public class JobengineMonitorApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(JobengineMonitorApplication.class, args);
}
}
I am using thymeleaf as the template engine
I solved this problem by putting favicon.ico in main/resource/static and adding this lines to my security config
httpSecurity
.authorizeRequests()
.antMatchers( "/favicon.ico").permitAll()
I have this with SpringBoot configuration too and is working
<link rel="shortcut icon" type="image/png" th:href="#{/img/favicon.png}"/>
And the favicon.png under resources/public/img
If anyone faces the same problem using newer version of Spring (in my case spring boot 2.4.4), here's the scenario that worked fine for me:
Put the favicon.ico in the /resources/static folder. I also tried to put it just in the /resourses/ folder and it worked fine as well, so do not worry about the folder that much.
Create a FaviconConfiguration in your configurations folder with the following content:
#Configuration
public class FaviconConfiguration {
#Bean
public SimpleUrlHandlerMapping customFaviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Integer.MIN_VALUE);
mapping.setUrlMap(Collections.singletonMap(
"/static/favicon.ico", faviconRequestHandler()));
return mapping;
}
#Bean
protected ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler requestHandler
= new ResourceHttpRequestHandler();
requestHandler.setLocations(Collections.singletonList(new ClassPathResource("/")));
return requestHandler;
}
}
In case you use Spring Security don't forget to add antMatcher for your favicon resource by adding the following code to your SpringSecurityConfiguration (as JaneXQ already mentioned above):
.antMatchers("/static/favicon.ico").permitAll()
Use link to your custom favicon explicitly in your html. To do that just put the following link in the <head> section of each of your html pages in the following way (I used thymeleaf in here):
<head>
...
<link rel="icon" type="image/ico" th:href="#{../static/favicon.ico}">
...
</head>
Put your favicon.png under src/main/resources/public and add this to your *.html page exactly in the header section
<link rel="shortcut icon" type="image/png" th:href="#{favicon.png}"/>
I found I had to put my favicon.ico file in:
src/main/resources/public
I saved my favicon which was a simple .png download as src/main/resources/static/favicon.ico
I couldn't get it to display until I tried another browser and it worked fine - so try clearing the browser cache, or try testing on another browser
Ok so this appears to be working now. Of course I managed to get it working just after ranting about it :).
Anyway, what i did was.
Remove #EnableWebMvc from the main class
For added ../ to the href depending on the urls e.g /index was fine but /edit/something.html was not
Sorry for wasting peoples time but hopefully this could be useful for another rookie like me
Try to replace th:href with href. It worked for me.
<link rel="icon" href="/favicon.ico" type="image/ico">
For some reason .ico format was not working. I just placed a png image instead and spring automatically picked the favicon.
I placed the png image in \src\main\resources\public
Spring boot + thymeleaf
I had the same issue and fix it removing #EnableAdminServer annotation

custom 404 sometimes missing stylesheet

I have a website hosted by siteground, and my custom 404 page is SOMETIMES missing it's stylesheet. I contacted support but they saw it working fine, assumed I just codded it wrong, and told me to get help form a professional web programmer... (of course I did test before contacting them to be sure it was not my fault)
Anyways, the way that siteground allowes you to create a custom 404 page is to add your html code to a form in your control panel, wich creates a 404.shtml in your main folder. The .shtml contains all your html. I have done the following test to narrow down the problem.
created a not404.html in my main folder with code copy pasted from my 404.shtml that is in the same folder. It loaded with my stylesheet applied no problem.
tried prompting a 404 page with both bad hyperlinks, and by typing pages that do not exist. When I am having the problem with my stylesheet missing, none of thease pages have css, but when I am not having the problem, they all have css applied.
What I think it is: I think that the 404.shtml is not really in my main folder, but just appears to be there, and sometimes that link breaks and the 404.shtml is actualy acessed somewere else on sitegrounds server, therefor the style link would not work properly. This would explain the intermittent css failor. But this is just logic, not understanding.
The only thing I can think of that would be wrong on my end is if there is more than one way to link stylesheets, and my way is a bit unstable. Here is the format I use.
<link type='text/css' rel='stylesheet' href='css/404.css'>
I know this might be a difficult question, but it would be pretty satisfying to figure this out and inform them of the problem with their site.
Change the href for the style tag to be absolute. So this:
<link type='text/css' rel='stylesheet' href='css/404.css'>
Becomes this:
<link type='text/css' rel='stylesheet' href='/path/to/css/404.css'>
css/404.css is a relative path. If the path is something like /path/to/nonextant/page it will try to load the stylesheet from /path/to/nonextant/page/css which does not exist.
Use an absolute path:
/css/404.css

Redirecting /Directory/Page.aspx to /Directory

I'm trying to prevent duplicate content SEO issues with a new page. It's the default page of a new directory, so by default will be served on either http://site.com/Directory or http://site.com/Directory/Default.aspx.
I like the simple, clean URL of http://site.com/Directory and want to make sure that http://site.com/Directory/Default.aspx does a 301 redirect to the clean URL so there are no duplicate content issues.
I thought this would be a simple fix:
if (Request.Path == "/Directory/Default.aspx")
RedirectPermanent("/Directory");
but I was surprised to find that Request.Path shows /Directory/Default.aspx even when the browser is pointed to "/Directory", thus the above code would produce an infinite loop.
It seems simple, but I can't find a way to distinguish which page is in the URL. Request.RawURL is no help.
Canonical URLS are a common requirement, so there should be a simple way around this. Anybody have any ideas?
Just use the canonical <link> tag. It tells Google which page is the "main" page and doesn't require a lot of programming or redirecting.
<link rel="canonical" href="/Directory" />

favicon not show if page have querystring

Favicon is displayed fine if the URL have no querystring, while it does't show if the URL contain querystring!!
Here are the page without querystring:
www.NoonOffer.com
Here are the page with querystring
http://www.noonoffer.com/Offers.aspx/7/Pay-LE-50-and-get-50-discount-on-PMP-course
Any suggestions?
Your url doesn't appear to be correct. Something in your code is not putting the proper URL in the HREF.
It is:
http://www.noonoffer.com/Offers.aspx/7/favicon.png
Should be:
http://www.noonoffer.com/favicon.png
EDIT: Not sure with .NET, but using Core Tag Library with Java Spring MVC a "/" at the front of the URL will make a difference when using relative paths.
This would append it to the end of the URI I believe:
<c:url value="/favicon.png">
And I believe this would append it to the context root like you want.
<c:url value="favicon.png">
This may not be the exact issue, but I hope it will help.

Getting static web context resources to work on both bookmarkable and non-bookmarkable Wicket pages

In a Wicket 1.4 app, I have some static CSS & JS resources under [project root]/WebContent/css and [project root]/WebContent/js respectively.
My Wicket HTML files are in src/resources/fi/company/product/pages with corresponding Java classes in src/main/fi/company/product/pages. (In the resulting WAR file, the HTML & property files are of course in the same places as Java classes.)
The HTML files contain references to the resources such as:
<head>
<link rel="stylesheet" type="text/css" href="css/main.css"/>
<script type="text/javascript" src="js/calendar.js"></script>
</head>
This works fine everywhere (or so we thought until recently). NB: my Java code does not reference these resources at all.
Looking at the source of a rendered page (whose URL is e.g. http://localhost:8080/report/42.4 or http://localhost:8080/?wicket:interface=:6:::: ), the resource reference appears as:
<link rel="stylesheet" type="text/css" href="../css/main.css"/>
However, we just noticed that when the app is deployed somewhere else than (Tomcat) root, the resources break on non-bookmarkable pages.
In other words, when the URL is e.g.
http://localhost:8080/foobar/?wicket:interface=:2::::
and a page refers to
<link rel="stylesheet" type="text/css" href="../css/main.css"/>
...the browser tries to fetch the resource at the invalid URL
http://localhost:8080/css/main.css
Now, what is the simplest (yet non-hacky) way to get these static resources working, regarless of the deployment path?
I could switch to using bookmarkable pages exclusively (which would require changing the constructors of the pages), but I suppose that shouldn't be necessary...
Edit: Looks like I got CSS resources working (on most places) simply by using <wicket:link>, as advised in this answer:
<head>
<wicket:link>
<link rel="stylesheet" type="text/css" href="css/main.css"/>
</wicket:link>
</head>
However, now the CSS references are broken on a page with an URL like http://localhost:8080/foobar/report/42.9
Wicket is trying to do something strange with the "css/main.css" path:
ERROR org.apache.wicket.RequestCycle - Can't instantiate page using constructor public fi.company.product.pages.ReportPage(org.apache.wicket.PageParameters) and argument 0 = "css" 1 = "main"
org.apache.wicket.WicketRuntimeException: Can't instantiate page using constructor public fi.company.product.pages.ReportPage(org.apache.wicket.PageParameters) and argument 0 = "css" 1 = "main"
at org.apache.wicket.session.DefaultPageFactory.createPage(DefaultPageFactory.java:212)
at org.apache.wicket.session.DefaultPageFactory.newPage(DefaultPageFactory.java:89)
at org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.newPage(BookmarkablePageRequestTarget.java:305)
Edit 2: Actually I'm not sure if <wicket:link> is the right solution here, since these resource files are not "class path resources". I guess my question is, can you make this work while still using web context resources (i.e., without making these class path resources)?
Right, I solved it, and the solution turned out to be very surprising.
Earlier I wrote:
A curious thing is that without any changes, it seems I can no longer
reproduce the problem...
That wasn't quite true, as I had made one small change (that I thought was inconsequential): I had deleted a file WebContent/index.jsp which in our project was a remnant that served no purpose.
Once it dawned on me that this could have fixed it, I did some more testing, and indeed:
For static resources to work as expected, you must not have an index.html or index.jsp file in the root web content directory (i.e., the parent of the CSS and JS resource dirs), as that in some cases breaks ../ references.
This probably isn't even Wicket-specific, but perhaps it is Tomcat-specific—if anyone knows more, feel free to chime in. I'm dubious whether this question ever helps anyone else, but still, glad I got it working!

Resources