在实体框架4.1中,是否有更好的方法使用URL查找子页面?

发布于 2024-12-11 09:29:46 字数 2201 浏览 0 评论 0原文

我正在为网站设置页面和子页面系统。因此,假设有一个“索引”页面和一个“子”页面。要访问子页面,URL 将如下所示:“Index/Sub”。

可能还有另一个页面的 URL 部分带有“Sub”。 IE:“索引/子”和“另一页/子”。 在 ASP.NET MVC 中,我获取一个 URL 并用正斜杠将其拆分为一个数组。然后我使用 EF4.1 来查找正确的页面实体。

当我查看代码时,它可以工作,但我想知道是否可以减少对数据库的调用次数。这是我的测试动作:

    public string Index(string url)
    {
        // split url into array
        string[] slugs = url.Split('/');
        string currentSlug = slugs[0];

        // set starting page and loop
        Page page = db.Pages.FirstOrDefault(x => x.Slug == currentSlug);
        for (var i = 1; i < slugs.Count(); i++)
        {
            if (!string.IsNullOrEmpty(slugs[i]) 
                && page != null 
                && page.Children != null)
            {
                page = page.Children.FirstOrDefault(x => x.Slug == slugs[i]);
            }
        }

        // if null return 404
        if(page == null) throw new HttpException(404, "HTTP/1.1 404 Not Found");

        // return page
        return page.Label;
    }

这可怕还是正常?有没有办法减少数据库调用的潜在数量?

-感谢您的任何提示

-编辑

这是我的页面模型仅供参考

public class Page
{
    public Guid PageId { get; set; }

    [Required(ErrorMessage = "URL is required")]
    [StringLength(30, ErrorMessage = "Must be less than 30 characters")]
    public string Slug { get; set; }

    [Required(ErrorMessage = "Label is required")]
    [StringLength(30, ErrorMessage = "Must be less than 30 characters")]
    public string Label { get; set; }

    [StringLength(100, ErrorMessage = "Must be less than 100 characters")]
    public string Title { get; set; }

    [StringLength(255, ErrorMessage = "Must be less than 255 characters")]
    public string Keywords { get; set; }

    [StringLength(300, ErrorMessage = "Must be less than 300 characters")]
    public string Description { get; set; }

    public DateTime DateCreated { get; set; }

    public DateTime DateModified { get; set; }

    public string LayoutName { get; set; }

    public virtual ICollection<Content> Contents { get; set; }

    public virtual Page Parent { get; set; }

    public virtual ICollection<Page> Children { get; set; }
}

I am setting up a page and sub-page system for a site. So, let's say there is an "Index" page, with a "Sub" page. To access the sub page the URL would look like this: "Index/Sub".

There could another page with "Sub" for the URL part. IE: "Index/Sub" and "AnotherPage/Sub".
In ASP.NET MVC, I am taking a URL and splitting it into an array on forward slashes. Then I use EF4.1 to find the correct page entity.

As I look at the code, it works, but I'm wondering if I can reduce the number of calls to the database. Here's my test action:

    public string Index(string url)
    {
        // split url into array
        string[] slugs = url.Split('/');
        string currentSlug = slugs[0];

        // set starting page and loop
        Page page = db.Pages.FirstOrDefault(x => x.Slug == currentSlug);
        for (var i = 1; i < slugs.Count(); i++)
        {
            if (!string.IsNullOrEmpty(slugs[i]) 
                && page != null 
                && page.Children != null)
            {
                page = page.Children.FirstOrDefault(x => x.Slug == slugs[i]);
            }
        }

        // if null return 404
        if(page == null) throw new HttpException(404, "HTTP/1.1 404 Not Found");

        // return page
        return page.Label;
    }

Is this scary or alright? Is there a way to reduce the potential number of db calls?

-Thanks for any tips-

Edit

Here's my Page model FYI

public class Page
{
    public Guid PageId { get; set; }

    [Required(ErrorMessage = "URL is required")]
    [StringLength(30, ErrorMessage = "Must be less than 30 characters")]
    public string Slug { get; set; }

    [Required(ErrorMessage = "Label is required")]
    [StringLength(30, ErrorMessage = "Must be less than 30 characters")]
    public string Label { get; set; }

    [StringLength(100, ErrorMessage = "Must be less than 100 characters")]
    public string Title { get; set; }

    [StringLength(255, ErrorMessage = "Must be less than 255 characters")]
    public string Keywords { get; set; }

    [StringLength(300, ErrorMessage = "Must be less than 300 characters")]
    public string Description { get; set; }

    public DateTime DateCreated { get; set; }

    public DateTime DateModified { get; set; }

    public string LayoutName { get; set; }

    public virtual ICollection<Content> Contents { get; set; }

    public virtual Page Parent { get; set; }

    public virtual ICollection<Page> Children { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や三分注定 2024-12-18 09:29:46

我当然会在每个 Page 上包含完整的页面路径,这样您就可以在一个数据库调用中执行查询。 (是的,那么数据将不会在数据库中完全规范化,但有时为了性能我们必须做出牺牲)。只要确保您有一个策略,可以在父页面发生更改时更新子页面。

如果您不喜欢页面上完整路径列的想法,也许您应该缓存它们解析到的路径和页面 ID,而不是每次都询问数据库(记住有一个缓存清除策略)。

I'd certainly include the full page path on each Page, so you can do the query in one db call. (Yes, then the data will not be fully normalised in the DB, but that a sacrifice we sometimes have to make for the sake of performance). Just make sure you have a strategy to update sub pages when ever a parent page changes.

If you dislike the idea of a full path column on the pages, perhaps you should cache the paths and the page ID's they resolve to, instead of asking the DB each time (Remember to have a cache clearing strategy).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文