使用 URL 路径从数据库生成页面标题和元标记

Generate Page Title and Meta Tags from database using URL path

我想通过从数据库中获取名称来动态更改标题标签。此内部数据库已在相同 table 中具有名称和 ID,但我遇到了麻烦。

我想做的是从 Url 路径获取 QRLinkCode,然后 return 与该 QRLinkCode 关联的名称以填充视图源中的标题元标记。

这是我到目前为止所写的。

     public class PageMetaDetails
            {
                public static string UpdateMetaDetails (string urlpath)
                {
                    //--- StringBuilder object to store MetaTags information.
                    StringBuilder sbMetaTags = new StringBuilder();
        
                    //--Step1 Get data from database.
        
                    using (TemplateDBEntities db = new TemplateDBEntities())
                    {
                       
                        var qrLinkName = "";
                        
                        
                         if (urlpath.StartsWith("/ItemList"))
                        {
                            //get qrLink code out of the url
                            var qrLinkId = urlpath.Substring(urlpath.Length - 6);
        
        
                            //hit the database for this qr link by id
                            var qrLink = db.QRLinks.FirstOrDefault(x => x.QRLinkCode == qrLinkId);
                            qrLinkName = qrLink.Name;
                            
                            
        
                        }
                        
        
                        //---- Step2 In this step we will add <title> tag to our StringBuilder Object.
                        //sbMetaTags.Append(qrLinkName);
                        //sbMetaTags.Append(Environment.NewLine);
        
                        
                        return qrLinkName;
                    }
        
                    
                }
                
               
            } 

There is code that will go in between this that's why its not there are extra {} but I just want to get this part working first.

我在下面的 Layout.cshtml 中也有这个,用于将名称路由到标题元标记。

<title>
        @{string title = Url.RequestContext.RouteData.Values["controller"] + "/" + Url.RequestContext.RouteData.Values["action"];
            @(Html.Raw(Models.PageMetaDetails.UpdateMetaDetails(title)));} </title>

出于某种原因,它没有 returning 与 ID 关联的名称。如果有人有任何想法,请告诉我哪里出错了。

好吧,显然 url 路径字符串有问题,所以我更正了我的输入。


                if (HttpContext.Current.Request.Url.AbsolutePath.StartsWith("/ItemList"))
                {
                    //get qrLink code out of the url
                    //var qrLinkId = urlpath.Substring(urlpath.Length - 6);
                    
                    var qrLinkId = HttpContext.Current.Request.Url.AbsolutePath.Substring(HttpContext.Current.Request.Url.AbsolutePath.Length - 6);
                   

                    //hit the database for this qr link by id
                    var qrLink = db.QRLinks.FirstOrDefault(x => x.QRLinkCode == qrLinkId);
                    qrLinkName = qrLink.Name;
                    // var orgId = qrLink.FKId;

                    

                }


允许 return 与 url 子字符串中的二维码关联的标题。