Url in ASP.NET MVC layout

2.8k views Asked by At

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" [email protected]("~/Scripts/bootstrap.min.js")></script>
@RenderSection("Script", false)

Thank you in advance for any help.

EDIT: I included full layout.

1

There are 1 answers

1
Parv Sharma On
@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