turbo88
turbo88

Reputation: 453

Issue with MVC4 applications logo

I am trying to include a logo to my MVC 4 site. Upon clicking on that logo, it should navigate to the home screen. I am using the following piece of code in the _Layout.cshtml file to achieve this.

<a href='<%= Url.Action("Index","Home") %>'> <img src="../Images/logo.JPG" /></a>

In internet explorer, if I click on the logo, nothing seems to be happening. But in Google Chrome, I am getting the error as Bad Request - Invalid URL . The URL content is

http://localhost:3347/Home/%3C%=%20Url.Action(%22Index%22,%22Home%22)%20%%3E

Please let me know if I am making any mistake here.

Upvotes: 0

Views: 44

Answers (2)

Chris Pratt
Chris Pratt

Reputation: 239380

Your problem is that it's literally setting the link to <%= Url.Action("Index","Home") %>, not the results of that call. Razor templates don't use the <%= %> syntax. Try the following instead:

<a href='@Url.Action("Index","Home")'> <img src="../Images/logo.JPG" /></a>

Also, when dealing with static assets, you should use Url.Content, which will prefix the correct application path for you so you don't end up breaking links in various deployment scenarios:

<img src="@Url.Content("~/Images/logo.jpg")" />

Upvotes: 1

Jason Evans
Jason Evans

Reputation: 29186

Try

<a href='@Url.Action("Index","Home")'> <img src="../Images/logo.JPG" /></a>

or you can use

<a href='/'> <img src="../Images/logo.JPG" /></a>

If the root of your site is '/' i.e. it still ends up going to Home/Index anyway, if that's the default configured in RouteConfig.RegisterRoutes().

Upvotes: 2

Related Questions