如何在ASP.NET 4.0 Webforms中动态制作SEO友好的无扩展名的URL

How to make SEO friendly extensionless urls dynamically in ASP.NET 4.0 webforms
2021-06-10
  •  译文(汉语)
  •  原文(英语)

首先,如果这个问题已经提出,我深表歉意,但是我找不到解决我问题的方法.我正在ASP.NET 4.0 Wepforms网站上工作,管理员将在其中上传图片,并且该图片将被视为帖子,例如图片博客.访客可以使用fb注释对每张照片进行注释并喜欢.目前,IAM对每个帖子(图片)使用查询字符串.但是现在我想把每个URL都当作SEO.所以基本上我有两件事要处理:

  1. 为管理员上传的每张图片制作唯一的URL,URL应该是其标题,例如,与博客引擎或nopCommerce相同的"www.example.com/posts/mexican-bulldog".当前,iam在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"); 
    }    
    

    上面的代码不能解决我的问题,在其中我必须静态分配网址.

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

提前谢谢!

速聊1:
要生成块,请检查以下内容:stackoverflow.com/questions/2920744/url-slugify-alrogithm-in-c 根据您数据库中现有类似块的数量,为唯一的块添加编号1,2,3的逻辑.
解决过程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.

希望这对您有用.

速聊1:
thanx快速答复,对我来说似乎是解决方案,在实施后会让您知道.

First of all i apologize if this question is already asked, but i am unable to find any solution for my problem. I am working on a ASP.NET 4.0 Wepforms website in which admin will upload a picture and it will be considered as a post, like a pictures blog. Visitors can comment each picture using fb comments and can like it. Currently iam using query strings for each post(picture). But now i want to make each url as a SEO freindly. So basically i have two things to deal with:

  1. Make Unique URL for each Picture that admin uploads , url should be its title e.g "www.example.com/posts/mexican-bulldog" same as blogengine or nopCommerce. Currently iam using the following code in 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"); 
    }    
    

    the above code is not solving my problem, in it i have to assign urls statically.

  2. want to navigate between posts i.e next and previous

thanx in advance !

Talk1:
To generate slug check this out: stackoverflow.com/questions/2920744/url-slugify-alrogithm-in-c Add logic of numbering 1,2,3 for unique slugs depending upon your existing number of similar slug in database.
Solutions1

Please look a simple way I have just implemented as per your requirement. There may be more ways to do the same.

I created a class: Images

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;
        }
    } 

Register the route and initializes the image collection at application startup:

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;
            }
        }

This is just a sample demonstration. The main idea here was to handle RouteData.Values["Name"].ToString() to handle dynamic urls.

Hope this will be useful for you.

Talk1:
thanx for a quick reply, seems like solution to me, will let you know after implementing.
转载于:https://stackoverflow.com/questions/19063568/how-to-make-seo-friendly-extensionless-urls-dynamically-in-asp-net-4-0-webforms

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入