Anchors / Custom Route with '#' in URL

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
Hi I want to use anchors (html) to jump directly to a product on the categorypage. For that I created a new route which adds the anchor to the end of the URL:

routes.MapLocalizedRoute("MyCategoryRoute",
        "c/{categoryId}#{SkuAnchor}",
        new { controller = "Catalog", action = "Category", SeName = UrlParameter.Optional },
        new { categoryId = @"\d+" },
        new[] { "Nop.Web.Controllers" });


Everything works OK, except that the '#' is transformed to '%23' So my URL is 'http://localhost:2148/c/21%230_098' instead of 'http://localhost:2148/c/21#0_098'.

How can I keep the '#' in there?
11 years ago
(Not strictly a nopcommerce question here)

I think the way you call it (e.g. @Url.RouteUrl()) will sanitize the url no matter what.

I'm afraid you'll have to Replace("%23", "#") on the caller if you want to have it working, that way you can pass the SkuAnchor as route parameter.

(you can always place the Replace call on an extension method that do this for you)

Or you can simply attach the anchor on the client side, if you don't necessarily need to pass it in as url parameter

href="@Url.RouteUrl("MyCategoryRoute", new {CategoryId = category.Id})#@(SkyAnchor)"
11 years ago
Thanks.

After a lot of trying and some beer I found a simple solution:

Instead of using Html.BeginRouteForm I simply build the form-Tag myself and can then simply include the anchor/hashtag
<form method="post" enctype="multipart/form-data" action="@url" novalidate="novalidate">

The hashtag will not be sent to the server, so I needed to correct the route.

Of course this is kind of a nasty hack, since I don't directly use routes/actions, but it works for me ;-)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.