【问题标题】:How to make SEO friendly extensionless urls dynamically in ASP.NET 4.0 webforms如何在 ASP.NET 4.0 网络表单中动态制作 SEO 友好的无扩展 url
【发布时间】:2013-09-28 05:13:51
【问题描述】:

首先我很抱歉,如果这个问题已经被问到,但我无法为我的问题找到任何解决方案。 我正在开发一个 ASP.NET 4.0 Wepforms 网站,管理员将在该网站上上传图片,并将其视为帖子,就像图片博客一样。访问者可以使用 fb cmets 评论每张图片并喜欢它。目前我正在为每个帖子(图片)使用查询字符串。但现在我想让每个网址都成为 SEO 友好。所以基本上我有两件事要处理:

  1. 为管理员上传的每张图片制作唯一的 URL,url 应该是它的标题,例如“www.example.com/posts/mexican-bulldog”与 blogengine 或 nopCommerce 相同。目前我在 Global.asax 中使用以下代码:

        void Application_Start(object sender, EventArgs e) 
        {
                RegisterRoutes(RouteTable.Routes);
    
        }
        public static void RegisterRoutes(RouteCollection routeCollection)
        {
            routeCollection.MapPageRoute("RouteForCustomer", "Customer/{Id}", "~/Customer.aspx");
    
    
    
            routeCollection.MapPageRoute("RouteForHome", "home", "~/Default.aspx");
            routeCollection.MapPageRoute("RouteForTroll", "post", "~/post.aspx"); 
    }    
    

    上面的代码没有解决我的问题,我必须静态分配url。

  2. 想要在帖子之间导航,即下一个和上一个

提前感谢!

【问题讨论】:

标签: c# asp.net url-rewriting asp.net-routing


【解决方案1】:

请看一下我刚刚根据您的要求实施的简单方法。可能还有更多方法可以做到这一点。

我创建了一个类:图像

public class Images
    {      
        public string CurrentImage { get; set; }
        public string NextImage { get; set; }
        public string PreviousImage { get; set; }
        public string CurrentImagePhysicalName { get; set; }
        public Images(string currentImagePhysicalName, string Current, string Next, string Previous)
        {
            this.CurrentImagePhysicalName = currentImagePhysicalName;
            this.CurrentImage = Current;
            this.NextImage = Next;
            this.PreviousImage = Previous;
        }
    } 

在应用启动时注册路由并初始化图像集合:

public class Global : HttpApplication
    {
        public static List<Images> col = new List<Images>();
        private void GetImages()
        {
            // Build this collection as per your requirement. This is just a sample. 
            // Logic is to store current, next, previous image details for current displaying image/page. 
            // Hope while storing image each will have unique name before saving and will have all details in db like path, display name, etc.

            col.Add(new Images("orderedList0.png", "orderedList0", "orderedList1", ""));
            col.Add(new Images("orderedList1.png", "orderedList1", "orderedList2", "orderedList0"));
            col.Add(new Images("orderedList2.png", "orderedList2", "orderedList3", "orderedList1"));
            col.Add(new Images("orderedList3.png", "orderedList3", "orderedList4", "orderedList2"));
            col.Add(new Images("orderedList4.png", "orderedList4", "", "orderedList3"));
        }

        void Application_Start(object sender, EventArgs e)
        {
            GetImages();            
            RegisterRoutes(RouteTable.Routes);
        }

        public static void RegisterRoutes(RouteCollection routeCollection)
        {
            routeCollection.MapPageRoute("RouteForImage", "Posts/{Name}", "~/Posts.aspx");

        } 
}

Posts.aspx

protected void Page_PreRender(object sender, EventArgs e)
        {
            string currentImage = RouteData.Values["Name"].ToString();
            if (!String.IsNullOrEmpty(currentImage))
            {
                Images image = Global.col.Find(x => x.CurrentImage == currentImage);
                // Get Current Image URL where actually it is stored using from image variable and render / set image path where you want to using image.CurrentImagePhysicalName 


                // Set Next - Previous Image urls
                if (!String.IsNullOrEmpty(image.NextImage))
                {
                    hyperlink_next.Visible = true;
                    hyperlink_next.Text = image.NextImage;
                    hyperlink_next.NavigateUrl = GetRouteUrl("RouteForImage", new { Name = image.NextImage });                    
                }
                else
                    hyperlink_next.Visible = false;

                if (!String.IsNullOrEmpty(image.PreviousImage))
                {
                    hyperlink_previous.Visible = true;
                    hyperlink_previous.Text = image.PreviousImage;
                    hyperlink_previous.NavigateUrl = GetRouteUrl("RouteForImage", new { Name = image.PreviousImage });
                }
                else
                    hyperlink_previous.Visible = false;
            }
        }

这只是一个示例演示。这里的主要思想是处理RouteData.Values["Name"].ToString() 来处理动态url。

希望这对您有用。

【讨论】:

  • thanx 快速回复,对我来说似乎是解决方案,实施后会通知您。
猜你喜欢
  • 2013-09-28
  • 2012-09-25
  • 2010-12-24
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 2016-05-17
  • 2013-12-31
  • 2020-05-09
相关资源
最近更新 更多