重新路由使用friendlyUrl查询字符串 [英] Reroute query string using friendlyUrl

查看:718
本文介绍了重新路由使用friendlyUrl查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重写我的网址,当我进行搜索。但我不能连得细分出我的网址,或者没有段但我不knwo如何去改变它。

我如何努力获得细分 Find.aspx 页面加载:

 的IList<串GT;段= Request.GetFriendlyUrlSegments();
            的for(int i = 0; I< segments.Count;我++)
            {
                Label1.Text + = - +区段[I] + - ;
            }

这是只是为了测试如果它甚至找到1段,它没有。

我自己也尝试在这我RouteConfig设置是这样的:

 公共静态无效的RegisterRoutes(RouteCollection路线)
{
    VAR设置=新FriendlyUrlSettings();
    settings.AutoRedirectMode = RedirectMode.Permanent;
    routes.EnableFriendlyUrls(设置);    routes.MapPageRoute(,查找,〜/ Find.aspx);
    routes.MapPageRoute(查找,查找/ {}的结果,〜/ Find.aspx);
}

我要的网址从:

  www.site.com/Find?Result=Test

要这样:

  www.site.com/Find/Test

  www.site.com/Test

我呼像这样的链接的Response.Redirect(〜/ Find.aspx?结果=+搜索字符串)

我也想知道如果本地主机:XXXXX /默认意味着,当我最终买了我的域名起始页看起来像 www.sitename.com /默认?如果是的话我怎么能重新路由是公正 www.sitename.com

基本上只是想使我的网站更SEO。


解决方案

您需要以下行发表评论,那么它应该工作。

  routes.MapPageRoute(,查找,〜/ Find.aspx);
routes.MapPageRoute(查找,查找/ {}的结果,〜/ Find.aspx);

更多信息 - 的转寄此

这些行的目的


  1. routes.MapPageRoute(,的findxxx,〜/ Find.aspx); 是替换 Find.aspx 用的findxxx,这里的findxxx 是搜索引擎友好的名称。而且它不会任何参数发送到 Find.aspx

用法 - 提供搜索引擎友好的名称 Find.aspx 。要使用此,你需要点击URL - 的http://本地主机:63197 /的findxxx

<醇开始=2>
  • routes.MapPageRoute(查找,FindMore / {}的结果,〜/ Find.aspx); - 此行添加SEO friendlyness +提供的方式来传递参数给搜索引擎友好的URL。

  • 用法 - URL - 的http://本地主机:63197 / FindMore / ABC 。为了获得价值 - 你需要使用下面的 - Page.RouteData.Values​​ [结果]

    为什么它不工作 - 在你的情况,这两条线有搜索引擎友好的名称查找,这让混乱的路由引擎,然后失败。

    如何制定

    以下是网址,我都试过了。

    以下是输出,

    和我有评论如下。

    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.

    解决方案

    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.

    这篇关于重新路由使用friendlyUrl查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆