Reputation: 2419
I am trying to make use of URL routing in my web forms application to make urls more user friendly. I would like to use the following mapping for one of my pages:
/services --> common/services.aspx
/services/seo --> common/services.aspx#seo
/services/hosting --> common/services.aspx#web-hosting
Can anyone please help me write the correct routing for this scenario? I would basically want to redirect the user to the different sections on the same page with different URLs as listed above.
Some of the URLs which are already working are:
//About Us
routes.MapPageRoute("", "about", "~/common/about.aspx", false);
//Contact Us
routes.MapPageRoute("", "contact", "~/common/contact.aspx", false);
Only the first one of the following works-
//Services - works
routes.MapPageRoute("", "services/{service}", "~/common/services.aspx", false);
//does not work
routes.MapPageRoute("", "services/web-development", "~/common/services.aspx#web", false);
routes.MapPageRoute("", "services/seo", "~/common/services.aspx#seo", false);
routes.MapPageRoute("", "services/digital-marketing", "~/common/services.aspx#marketing", false);
Upvotes: 0
Views: 2637
Reputation: 2419
For anyone facing similar problem, here's what i did finally. I set the routing of all Urls to the same page-
//Services
routes.MapPageRoute("", "services", "~/common/services.aspx", false);
routes.MapPageRoute("", "services/web-development", "~/common/services.aspx", false);
routes.MapPageRoute("", "services/seo", "~/common/services.aspx", false);
routes.MapPageRoute("", "services/digital-marketing", "~/common/services.aspx", false);
And moved the logic to jump to the section of the page to javascript (using jQuery):
$(function () {
if (window.location.href.indexOf("web-development") > 0) {
$('html,body').animate({ scrollTop: $(".service-web-development").offset().top }, 0);
}
else if (window.location.href.indexOf("seo") > 0) {
$('html,body').animate({ scrollTop: $(".service-seo").offset().top }, 0);
}
else if (window.location.href.indexOf("digital-marketing") > 0) {
$('html,body').animate({ scrollTop: $(".service-marketing").offset().top }, 0);
}
});
This checks the url of the page and moves the window location to the appropriate section of the page.
Upvotes: 1
Reputation: 500
Web browsers will prevent the server-side (ASP.NET) from reading anything after the # fragment for security reason. so you will not be able to communicate between pages unless on the client-side (JavaScript)...
Upvotes: 0
Reputation: 1989
Here's an introduction to friendly urls: http://www.hanselman.com/blog/IntroducingASPNETFriendlyUrlsCleanerURLsEasierRoutingAndMobileViewsForASPNETWebForms.aspx
Upvotes: 0