ASP.NET 路由 - 带数据库查找的路由约束

发布于 2024-09-15 16:53:36 字数 1442 浏览 5 评论 0原文

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

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

由于路由区分大小写,是否有人有关于如何处理大写问题的提示?例如类别/饮料应该转发到类别/饮料

预先感谢您的任何帮助,很高兴加入 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()}}
                           );

I'm just starting out with C# and ASP.NET and have the following questions. I am working with code adapted from a couple different tutorials playing with Northwind and have gotten this far. The list of acceptable categories is currently hard coded in a string but I would like to look up the CategoryName in the database to verify that it exists.

Obviously the purpose of this is to ensure that users don't just type:
www.domain.com/Categories/AnyUrlWillWork and return a valid page.

Also does anyone have an tips of how they are dealing with capitalization issues since the routing is case sensitive? For example Categories/beverages should forward to Categories/Beverages ?

Thanks in advance for any assistance, and glad to be joining 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()}}
                           );

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

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

发布评论

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

评论(1

药祭#氼 2024-09-22 16:53:36

这是 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 的类对象。您需要返回一个布尔值,这就是我所做的。

Is this MVC? If so, why not route to a function, which will check the category name against your data store and redirect to an error page if such category doesn't exist?

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

A WebForms solution would be:

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

Where you pass an object of class implementing IRepository with your data access logic (using NHibernate, EntityFramework or your own DAL implementation). You need to return a bool value, and this is what I did.

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