【问题标题】:ASP.NET Routing - Route Constraint with database lookupASP.NET 路由 - 带有数据库查找的路由约束
【发布时间】:2010-08-24 23:35:50
【问题描述】:

我刚开始使用 C# 和 ASP.NET,并且有以下问题。我正在使用从与 Northwind 一起玩的几个不同教程改编的代码,并且已经走到了这一步。可接受的类别列表目前被硬编码在一个字符串中,但我想在数据库中查找 CategoryName 以验证它是否存在。

显然,这样做的目的是确保用户不只是键入:
www.domain.com/Categories/AnyUrlWillWork 并返回有效页面。

由于路由区分大小写,还有人知道他们如何处理大小写问题吗?例如 Categories/beverages 应该转发到 Categories/Beverages ?

提前感谢您的帮助,很高兴加入 Stack Overflow。

//Complex contraint class
public class EchoConstraint : IRouteConstraint
{
    public readonly string[] ValidMessages = { "Beverages", "Produce", "Confections", "Seafood" };

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        string message = values["CategoryName"] as string;
        return ValidMessages.Contains(message);
    }
}


//Routes
RouteTable.Routes.MapPageRoute(
                        "Category Route",                       // Route name
                        "Categories/{CategoryName}",            // Url pattern
                        "~/ShowProductsInCategory.aspx",        // Physical file
                        true,
                        new RouteValueDictionary            
                            {{"CategoryName", "Beverages"}},         //Sets default value if none is provided in URL
                        new RouteValueDictionary 
                            {{"CategoryName", new EchoConstraint()}}
                           );

【问题讨论】:

标签: c# asp.net database routing case-sensitive


【解决方案1】:

这是 MVC 吗?如果是这样,为什么不路由到一个函数,该函数将根据您的数据存储检查类别名称并在此类类别不存在时重定向到错误页面?

public ActionResult Index(string catName)
{
    if (string.IsNullOrEmpty(catName) || !MyCategoriesDataStore.Exists(catName))
        return RedirectToAction("CategoryError");

    // Load category data to be used from View

    return View();
}

WebForms 解决方案是:

    public class EchoConstraint : IRouteConstraint
    {
        private IRepository rep;
        public EchoConstraint(IRepository r) { rep = r; }

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            return rep.GetCategory(message) == 0;
        }
    }
.
.
.
                        new RouteValueDictionary 
                            {{"CategoryName", new EchoConstraint(myDataAccessRepo)}}
                           );

您通过数据访问逻辑(使用 NHibernate、EntityFramework 或您自己的 DAL 实现)传递实现 IRepository 的类的对象。你需要返回一个布尔值,这就是我所做的。

【讨论】:

  • 感谢您的回复。不幸的是,我还没有深入研究 MVC。这是我赶上之后的下一步……自从 1.1 天以来我就没有玩过 ASP.NET,所以这是全新的。我正在学习本教程顺便说一句:4guysfromrolla.com/articles/050510-1.aspx
  • 我认为你应该直接进入 MVC。 WebForms 中的路由并不像现在那样自然,恕我直言,WebForms 将随着 MVC 的出现而慢慢消亡。
  • 我正在考虑它,但我需要了解很多基础知识,而且我的培训/课程不包括 MVC。一旦我通过他们,我会和你一起跳上马车。
  • 我编辑了我的答案,以便为您提供一种使用 WebForms 执行此操作的方法,看看它是否有帮助
  • 感谢添加。这有点过头了,但这就是我跳进去的结果。到目前为止,我一直在阅读本教程:asp.net/data-access/tutorials/creating-a-data-access-layer-cs 并创建了一个返回类别名称( categoriesAdapter.GetCategories(); )的 DAL,但不确定如何在此类中继续和使用它。但是,我可以绑定到页面上的控件。
猜你喜欢
  • 2010-10-29
  • 1970-01-01
  • 2011-03-28
  • 1970-01-01
  • 2011-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多