1

I have created a HelloWorld Controller and it's corresponding view. All is working correctly when I go to http://localhost/HelloWorld

I am trying to add a menu item to the default MVC app. In the _Layout.cshtml file I have

<ul id="menu">
    <li>@Html.ActionLink("Home", "Index", "Home")</li>
    <li>@Html.ActionLink("Hello World", "HelloWorld", "")</li>
    <li>@Html.ActionLink("About", "About", "Home")</li>
</ul>

Note they only thing I added was the Hello World part. However when I click on the menu item it takes me http://localhost/Home/HelloWorld. How do I get it to go to just http://localhost/HelloWorld?

I'm really new to MVC and not exactly sure what I'm doing. A few google searches have mentioned modifying the Routes in the Global.asax.cs but that seems a bit weird and not sure where to start there. I've also tried using the ~ to get back to root which works in the old school asp.net pages but not here.

3 Answers 3

2

The definition for ActionLink is.

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName
)

Since you are trying to redirect to /HelloWorld, I'm assuming that your controller is HelloWorld and your action is Index. Using this information we can fill in the method.

Html.ActionLink(
    "Hello World",  // Link Text
    "Index",        // Action
    "HelloWorld",   // Controller
)
Sign up to request clarification or add additional context in comments.

2 Comments

To easy. Amateur mistake. Can't accept until 7 mins has past though. Will accept in a few mins. Thanks.
You're welcome. Sometimes we are so tired even the simplest things are difficult.
0

The syntax for ActionLink that you want is:

@Html.ActionLink(string linkText, string actionName, string controllerName)

You're passing in the ActionName and leaving the controllerName blank, which by default will assume it's an Action on the current controller (in this case, "Home").

try

@Html.ActionLink("Hello World", "HelloWorld", "HelloWorld")

assuming, that is, that HelloWorld in the name of the Action on your HellowWorldController. If the Action is something different, replace the second parameter with the appropriate name.

1 Comment

Since he wants the URL to be /HelloWorld the action has to be Index.
0

You can also use empty strings:

@Html.ActionLink("Home", "", "")

@Html.ActionLink("About Us", "About", "Home")

@Html.ActionLink("Contact me", "Contact", "Home")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.