0

I am trying to rewrite my URLs when I make a search. But I can't even get the segments out of my URL, or maybe there are no segments but then I dont knwo how to change it.

How I try to get segments in Find.aspx pageload:

IList <string> segments = Request.GetFriendlyUrlSegments();
            for (int i = 0; i < segments.Count; i++)
            {
                Label1.Text += "- " + segments[i] + " -"; 
            }

This is just to test if it even find 1 segment, which it does not.

I have also tried setting in it my RouteConfig like this:

public static void RegisterRoutes(RouteCollection routes)
{
    var settings = new FriendlyUrlSettings();
    settings.AutoRedirectMode = RedirectMode.Permanent;
    routes.EnableFriendlyUrls(settings);

    routes.MapPageRoute("", "Find", "~/Find.aspx");
    routes.MapPageRoute("Find", "Find/{Result}", "~/Find.aspx");
}

I want to change the URL from this:

www.site.com/Find?Result=Test

To this:

www.site.com/Find/Test

or

www.site.com/Test

I "call" the link like this Response.redirect("~/Find.aspx?Result=" + searchString)

I am also wondering if Localhost:xxxxx/Default Means that when I eventually buy a domain my startpage will look like www.sitename.com/Default? If so how can I reroute that to be just www.sitename.com?

Basically just want to make my site more SEO.

0

2 Answers 2

1
+50

You need to comment below lines, then it should work.

routes.MapPageRoute("", "Find", "~/Find.aspx");
routes.MapPageRoute("Find", "Find/{Result}", "~/Find.aspx");

More info -- Refer this.

Purpose of these lines

  1. routes.MapPageRoute("", "FindXXX", "~/Find.aspx"); is to replace Find.aspx with FindXXX, here FindXXX is SEO friendly name. And it does not send any parameter to Find.aspx .

Usage - It provides SEO friendly name to Find.aspx. To use this, you need to hit url - http://localhost:63197/FindXXX

  1. routes.MapPageRoute("Find", "FindMore/{Result}", "~/Find.aspx"); -- This line add SEO friendlyness + provides way to pass param to SEO friendly URL.

Usage - URL - http://localhost:63197/FindMore/abc. To get value - you need to use following - Page.RouteData.Values["Result"]

Why it was not working - In your case, both lines had SEO friendly name as Find and that made confusion to routing engine, and then failed.

How worked

Following is the url, I have tried. enter image description here

Following is the output,

enter image description here

And I have commented following.

enter image description here

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

9 Comments

Oooh, So that code somehow messed with FriendlyUrls. So how do I redirect to "Find" now? Before I used Response.redirect("Find.aspx?Result=" + query) . What should I replace this line with? My problem is not that I cant get values, I want to replace the ?Result= with a / in the URL so that it is SEO
I think you are trying to explain how I can get values from the URL. I want to make the URL more SEO friendly by removing ?Result= and replacing it with a / . So www.site.com/Find?Result=query becomes www.site.com/Find/query
Yes, you are right in last comment, it should be www.site.com/Find/query with code suggested by me.
But how? You did not change anything except comment the 2 lines? That does not work for me
That is the magic of FriendlyURL, just need to have those 3 line and DLL reference, it will make .aspx file as SEO friendly, It should work, just clean and rebuild project.
|
0

First Of All You have to mapPage Url Like This

Routes.MapPageRoute("RouteName", "User/Friendly/Page/Address", "~/OriginalPageAdress.aspx")


Routes.MatPageRoute("Find", "Find/{result}/", "~/Find.aspx")
 (/) Character must be place in the last of firendlyUrl b'coz if you enter some text with the space(s) then friendlyUrl will not work Properly.


Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

    //Response.RedirectToRoutePermanent("Search", New With {.paramName = "paramValue", ...})
    Response.RedirectToRoutePermanent("Find", New With {.result = "Search Value"})

End Sub

To Access "Search Value" enter the following code in "~/Find.aspx" Page :

Dim SearchValue as String = Page.RouteData.Values("result")

Response.Write(String.Format("Result For : {0}"), SearchValue)

For UrlSegments

dim Segm = Request.Urls.Segments(0)

Comments

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.