0

When you create a ASP.NET MVC 2 web application in Visual Studio, it gives you a default MVC site. It shows 2 menus [Home] and [About]. When you hover these menus it gives you this URL:

  • [Home] localhost:1234/
  • [About] localhost:1234/Home/About

Why is About under Home?

I want menu links like

  • [Home] localhost:1234/
  • [About] localhost:1234/About

How can I do this?

4 Answers 4

1

You've got a few options here, you can either map routes individually as Darin and Alexn are showing, or you can do something like this:

routes.MapRoute("Home", "{action}/{id}", new { controller = "Home", action = "index", id = "" });

See how there's no controller defined in the path? This should mean you can just say /About or /SomeOtherAction. The limitation here is that it's going to match a lot of routes, and they'll all go to the home controller unless you add more specific routes above.

Sign up to request clarification or add additional context in comments.

Comments

0

About is under Home because it's an action under the HomeController.

If you want to access about using /About, set up a new route in your Global.asax. Example:

routes.MapRoute("About", "About", new { controller = "Home", action = "About" }

Comments

0

You have two actions defined on the Home controller: Index and About. The Index action is defined as the default action for the site. For this reason http://localhost:1234 resolves to http://localhost:1234/Home/Index. You could set a new route to accomplish this:

routes.MapRoute(
    "About",
    "About",
    new { controller = "Home", action = "About", id = UrlParameter.Optional }
);

Comments

0

Putting about under any folder is a matter of personal choice.

If you would like to see the about file in a different folder structure the neat way of doing this in my opinion would be

1)Create a new folder in views called About. 2)Move the About.html file into this folder 3)Create a new file under Controllers folder called AboutController.cs 4) find the tag return View(); in the above created file and replace it with return View("About");

If you would like to add more files under About section(eg:about-autor.html,about-fundinghouse.html) you could continue to add it in the about folder and make the necessary change in the controller to return the respective view.

1 Comment

this way i will need to follow this steps every time i will create a new menu?

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.