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.

Green_qaue
  • 3,561
  • 11
  • 47
  • 89

2 Answers2

1

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

Community
  • 1
  • 1
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
  • 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 – Green_qaue Mar 07 '15 at 17:51
  • 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` – Green_qaue Mar 07 '15 at 17:57
  • Yes, you are right in last comment, it should be `www.site.com/Find/query` with code suggested by me. – Arindam Nayak Mar 07 '15 at 17:58
  • But how? You did not change anything except comment the 2 lines? That does not work for me – Green_qaue Mar 07 '15 at 17:58
  • 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. – Arindam Nayak Mar 07 '15 at 18:00
  • My URL still looks like this `www.localhost:xxxxx/Find?Result=query` – Green_qaue Mar 07 '15 at 18:04
  • I have already mentioned, you need to use `www.localhost:xxxxx/Find/query` with code suggested by me. – Arindam Nayak Mar 07 '15 at 18:05
  • I tried redirecting by using `response.redirect("~/Find/" + query.Text);` But then I dont know how to use `Page.RouteData.Values["Result"]` . I think you are skipping a step or 2 that are obvious to you maybe? But I have no clue, never done rerouting before. If you could show the code for how you actually send the query-parameter with the redirect that would be good. – Green_qaue Mar 07 '15 at 18:12
  • @MrCharli3 , forget you code, just follow what is written below `How worked`, it will work. I can not re-iterate the same thing in answer in comment, however to make it simple i am doing so --> comment last 2 line - hit url like this/ or redirect to `www.localhost:xxxx/Find/query` -- get that query value using - `IList segments = Request.GetFriendlyUrlSegments()` – Arindam Nayak Mar 07 '15 at 18:24
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)