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.
2
Answers
You need to comment below lines, then it should work.
More info — Refer this.
Purpose of these lines
routes.MapPageRoute("", "FindXXX", "~/Find.aspx");
is to replaceFind.aspx
with FindXXX, hereFindXXX
is SEO friendly name. And it does not send any parameter toFind.aspx
.Usage – It provides SEO friendly name to
Find.aspx
. To use this, you need to hit url –http://localhost:63197/FindXXX
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.
Following is the output,
And I have commented following.
First Of All You have to mapPage Url Like This
To Access “Search Value” enter the following code in “~/Find.aspx” Page :
For UrlSegments