Reroute query string using friendlyUrl

[亡魂溺海] 提交于 2019-12-25 07:56:31

问题


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.


回答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.

Following is the output,

And I have commented following.




回答2:


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)


来源:https://stackoverflow.com/questions/28855430/reroute-query-string-using-friendlyurl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!