How to hide the home controller path in action links - asp.net-mvc

Using the out of the box MVC application the action links under the Home controller are rendered as follows
#Html.ActionLink("Home", "Index", "Home") > /
#Html.ActionLink("About", "About", "Home") > /Home/About
How do i make all action links that falls into the HomeController to hide the "Home" in the link paths.
e.g
#Html.ActionLink("About", "About", "Home") > /About
#Html.ActionLink("Contact", "Contact", "Home") > /Contact
#Html.ActionLink("Sitemap", "Sitemap", "Home") > /Sitemap
#Html.ActionLink("Terms", "Terms", "Home") > /Terms
Thanks

You can set the controller in your route part and remove it from the url. Something like this:
routes.MapRoute("", "/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Look at this answer also.

You can create different routes in the RouteConfig class as followed:
routes.MapRoute(
name: "AboutUs",
url: "about-us",
defaults: new { controller = "Home", action = "AboutUs" }
);
This way, when the URL is /about-us it's going to call the AboutUs action in Home controller.

Related

ASP.NET MVC - keep links to the root folder

I am trying to link inside the same folder, I have the following structure
/Home/Index
/Home/About
/Home/Contact
When I start the webpage, I link to the Index, so I get the webpage on the screen: www.example.com.
Now I would like to link to another page so I get: www.example.com/Contact.html (or even better I would like to get www.example.com/Contact) however I get www.example.com/Home/Contact.
I use this as an action link:
<li class="pure-menu-item pure-menu-selected">#Html.ActionLink("Contact us", "Contact", "Home")</li>
This is my route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
What could I change to get the desired result?
Decorate you Contact action with a RouteAttribute and pass it the desired route as parameter (i.e. "Contact")
Edit
Here's an example HomeController using the RouteAttribute:
public class HomeController
: Controller
{
public IActionResult Home()
{
return this.View();
}
[Route("Contact")]
public IActionResult Contact()
{
return this.View();
}
}
Note that you can use the RouteAttribute on Controllers, too. For instance, if I added a Route("Test") attribute on the HomeController, all of my controllers actions would look like: "/Test/[ActionRoute]".
In your views, you can use the following syntax, instead of using the old #Html.ActionLink tag helper:
<li class="pure-menu-item pure-menu-selected">
<a asp-controller="Home" asp-action="Contact">Contact Us</a>
</li>
In my opinion, those attribute tag helpers are way cleaner and html friendly ;)
i was able to fix it with some good reading and searching and this is what i came up with:
i added this to the routeConfig for each link to a html page:
routes.MapRoute("Index", "Index", new { controller = "Home", action = "Index" });
routes.MapRoute("About", "About", new { controller = "Home", action = "About" });
routes.MapRoute("Contact", "Contact", new { controller = "Home", action = "Contact" });
and instead of using an action link i use a route link:
<li class="pure-menu-item">#Html.RouteLink("About us", "About")</li>
this gives the desired result: www.example.com/About

Routing issue with MVC

I'm working with MVC 3 and have an issue. Instead of giving mydomain/mydirectory/item like I expected I get this:
mydomain/mydirectory/list?animal=quack.
Here's the route in the global
//Default route mapping
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = #"[^\.]*", action = #"[^\.]*" }
);
Code showing how I'm building the link:
<div id="main-content" title="AnimalBox" style="float:none;">
<% Html.DataList(Model.PriceListAnimals).Columns(7).Item(item =>
{
item.Template(galleryImage =>
{%>
<div style="margin-left:20px; line-height:150%;">
<span><%= Html.ActionLink(galleryImage.AnimalName,"List",new { #animal = galleryImage.AnimalName }) %></span>
</div>
<% });
}).Render(); %>
</div>
Any ideas?
You have to define a route for special case routing like your instance. Since you are passing 'animal' as a parameter, you should create a route to handle that instance. In your global.aspx (above the default route), create something like below:
routes.MapRoute(
"Animal", // A distinct Route name
"{controller}/{action}/{animal}", // URL with parameters
new { controller = "MyDirectory", action = "List"}
);
This will define a route to the MyDirectory controller on the action list which has an animal parameter which is NOT OPTIONAL. Defining routes is what enables you to generate clean URLs from the html helpers and other methods (redirect to action, etc).
The overload for Html.ActionLink is:
Html.ActionLink("linkText", "actionName", "controller", object routeValues, object HtmlAttributes)
From what you've said, mydirectory = controller, and List = action, correct? If so, try:
<%= Html.ActionLink(galleryImage.AnimalName, "List", "mydirectory", new { #id = galleryImage.AnimalName }, null) %>
this should produce:
quack

HtmlHelper ActionLink omits area (asp.net mvc 4)

I have problem with generation of links
I have an Area named "Administration", it also has a HomeController as a root,
In the masterpage
<li>#Html.ActionLink("Home", "Index", "Home")</li>
<li>#Html.ActionLink("Admin", "Index", "Home", new {area = "Administration"})</li>
Also I inherited From Route, and the method GetVirtualPath accepts the parameter values
which omits area key, and passes only controller and action.
Where did I go wrong?
You use incorrect overload. You should be using LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, Object, Object) (note the last parameter at the end)
#Html.ActionLink("Admin", "Index", "Home", new {area = "Administration"}, null)
<li>#Html.ActionLink("Home", "Index", "Home", new {area = ""}, null)</li>
<li>#Html.ActionLink("Admin", "Index", "Home", new {area = "Administration"}, null)</li>
The problem solved, and was in DataTokens. I was defining area only for areas, but not for controllers in the root.
Now it works. Thanks!

wrong url with html.actionlink

I was made paging system. Everything is OK.
After click 2nd page all homepage links changing.
#Html.ActionLink("Home page", "Index", "Home") //This is standard routing without values.
I was added paging links to end of page.
#Html.ActionLink("2", "Index", "Home", New With {.id = 2}, Nothing) //This works good too.
My problem is when I click to 2nd or more page (e.g : www.site.com/Home/Index/2) my all homepage links converting to
Home page
same this.
How I can resolve this problem?
When you click on the 2nd page the {.id = 2} will be part of your RouteData. And because your routing probably looks like this: (in Gloabal.asax)
routes.MapRoute( _
"Default", _
"{controller}/{action}/{id}", _
New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _
)
ASP.NET MVC will use this route for the generated links. So it will include Id = 2. To fix this you need to explicitly override it when it's not needed:
#Html.ActionLink("Home page", "Index", "Home", New With {.id = ""}, Nothing)

Html.ActionLink for non-standard routing

I have a route definition like this:
routes.MapRoute(
"Pagesize",
"{controller}/{action}/pagesize/{pagesize}",
new { controller = "Home", action = "Index", pagesize = 10 }
);
When I use
<%= Html.ActionLink("MyText", "myaction", new { pagesize = 10 }) %>
it renders as
MyText
I can understand I am misusing ActionLink since I have /pagesize/ in between. How can I correctly use it to create the link?
MyText
Please note that I am using mvc RC2 and no other helper libraries. The generic ActionLink no longer exists in RC2.
Try:
<%= Html.RouteLink("MyText", "Pagesize", new { controller = "Home", action = "Index", pagesize = 10 })%>
have you tried specifying the defaults in the map route command
routes.MapRoute("Pagesize",
"{controller}/{action}/pagesize/{pagesize}",
new {pagesize = 10 },
new { controller = "Home", action = "Index" });

Resources