I have moved the Visual Studio 2013 template 'Home' controller into its own Area.
I am trying to make an ActionLink that points to an action on the HomeController.
However, instead of the link being rendered as :
www.site.com/Home/ActionName
I want it to render as
www.site.com/ActionName
(For all the actions in the controller).
This way the root of my site doesn't contain 'Home' in the links.
I am trying to deploy my routes using Attribute Routing, however I am lost as to how I do this, any point in the right direction would be appreciated.
Ok, so I figured it out:
On my controller to be used as the site root (for instance "HomeController"):
[RouteArea("Home")] // Area
[Route("Home")] // Controller
public class HomeController : Controller
{
[Route("~/")] // Rendered path of the Index Action www.site.com/
public ActionResult Index()
{
return View();
}
[Route("~/About")] // Rendered path of the About Action www.site.com/About
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
// ...And with no routing attribute on the Contact Action below,
// The rendered path will remain in the format:
// www.site.com/Home/Home?action=Contact
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
ActionLink examples (from above code):
This will now render as www.site.com
@Html.ActionLink("Home", "Index", "Home")
This will render as www.site.com/About
@Html.ActionLink("About", "About", "Home")
This will render as www.site.com/Home/Home?action=Contact
@Html.ActionLink("Contact", "Contact", "Home")
I hope this helps someone else. Thanks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With