wrong url with html.actionlink - asp.net-mvc

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)

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

how to replace & from id in mvc 4?

I have mvc 4 project. Its a site showing data in the form of table with many a link. I am using
<%: Html.ActionLink("Edit", "EditReport", "phed", New With {.rpid = item.id,.returnUrl = Request.Url.PathAndQuery}, New With {Key .Class = "ico edit"})%>
My reoute.config is
routes.MapRoute( _
name:="Default", _
url:="{controller}/{action}/{id}", _
defaults:=New With {.controller = "phed", .action = "Index", .id = UrlParameter.Optional} _
)
It shows a link as
example.com/phed/EditReport?rpid=2&returnUrl=phed
How can i route my request like
example.com/phed/EditReport?rpid=2/returnUrl=phed
I want to replace this '&' Ampersand in id with slash / or any other character?
No need to change route, just pass url llok like:
example.com/phed/EditReport/2/returnUrl=phed
for that code like :
<%: Html.ActionLink("Edit", "EditReport", "phed", New With {.id= item.id,.returnUrl = Request.Url.PathAndQuery}, New With {Key .Class = "ico edit"})%>
Try this code, Note tested
<%: Html.ActionLink("Edit", "EditReport", "phed", New With {.rpid = UrlHelper.Encode(item.id+"\\returnUrl="+Request.Url.PathAndQuery)}, New With {Key .Class = "ico edit"})%>

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!

How to hide the home controller path in action links

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.

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