0

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);
2
  • What you have tried? Commented Apr 24, 2015 at 4:09
  • @RahulSingh I have used the following routes, but only the first one works, others throw 404 error 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); Commented Apr 24, 2015 at 4:39

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Here's an introduction to friendly urls: http://www.hanselman.com/blog/IntroducingASPNETFriendlyUrlsCleanerURLsEasierRoutingAndMobileViewsForASPNETWebForms.aspx

1 Comment

I have already got some pieces of it working, the only thing i can't seem to figure out is using same url redirect the user to different sections of a page just like normal url request of www.abc.com#xyz
0

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)...

1 Comment

The only thing i would use this hash(#) for is to let client display appropriate section of the page to the user just like if he sent this request www.abc.com#xyz (show the area where anchor has the name xyz). Is it not doable using friendly urls?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.