1

I have the following problem. I created a layout and two views that use it. Each view uses different controller. Part of the layout is navigation with site name. What I want to do is to make site name link to always point to Home/Index action. It works when I click site name on Home controller view, but when I go to other site with different controller it throws an error.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

I use default route settings.

Requested URL: /index.html

Below is part of the template code.

@using System.Web.Script.Services

<title>Galeria zdjęć</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Art Mission</title>
<!-- Bootstrap -->
<link href="~/Css/bootstrap.min.css" rel="stylesheet" />
<link href="~/Css/style.css" rel="stylesheet" />
@RenderSection("Style", false)

<!-- Navbar
================================================== -->
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="@Url.Action("Index", "Home")">ArtMission</a>
        </div>
        <div id="bs-example-navbar-collapse-1" class="collapse navbar-collapse">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="@Url.Action("Show", "Gallery")">Galeria</a></li>
                <li><a href="#oferta">Oferta</a></li>
                <li><a href="#contact">Kontakt</a></li>
            </ul>
        </div>
        <!--/.nav-collapse -->
    </div>
</div>
<!-- /.navbar -->

 @RenderBody()

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script type="text/javascript" src=@Url.Content("~/Scripts/bootstrap.min.js")></script>
@RenderSection("Script", false)

Thank you in advance for any help.

EDIT: I included full layout.

pawel.podsiadly
  • 171
  • 2
  • 13
  • `/index.html` will look for a physical file called `index.html`. The routing framework is bypassed for URLs with file extensions. To get the `HomeController.Index` action, you would have to use the URL `/Home/Index`, `/Index`, or just `/` (based on the default route). – Chris Pratt Aug 05 '14 at 21:08
  • I just noticed that when I hover over link it shows: localhost:port/Gallery/index.html. I don't understand why it shows different controller than what I set in @Url.Action("Index", "Home"), and why .html is added. – pawel.podsiadly Aug 05 '14 at 21:24
  • You might have a missing or incomplete tag somewhere that's causing the URL to be jacked up as the browser tries to "fix" the markup. Or, perhaps some JavaScript on page is misbehaving. An easy way to check that is to just disable JavaScript in your browser and see if the URL is the same. Also, check the actual rendered source (Right Click -> View Source). See if the URL is right there. – Chris Pratt Aug 05 '14 at 23:20

1 Answers1

-1
@Url.Action("Index", "Home")

will always point to the Index Action in Home Controller no matter where you are on your site.

your problem probably is that you have included somewhere in your Layout @Url("index") so replace it with @Url("Index", "Home")

@Url("Index") will work fine till you are retrieving view from any action in Home Controller but as you go to a different Controller this will break and will not point to /Home/Index which by the way if you are using default routing should be only /Home

Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
  • I updated the code in question and included full layout code. I don't use @Url.("index"). The way it works now is: I run home/index, page loads fine, I hover over link and it shows localhost:port. I click link to gallery which is a view returned from different controller. Page loads fine, but when I hover over the same link as on the previous site it shows: localhost:port/Gallery/index.html. Any ideas? – pawel.podsiadly Aug 05 '14 at 21:29