Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET路由-带有数据库查找的路由约束_C#_Asp.net_Database_Routing_Case Sensitive - Fatal编程技术网

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

C# ASP.NET路由-带有数据库查找的路由约束,c#,asp.net,database,routing,case-sensitive,C#,Asp.net,Database,Routing,Case Sensitive,我刚从C#和ASP.NET开始,有以下问题。我正在使用从两个不同的教程改编的代码来使用Northwind,并且已经走到了这一步。可接受类别的列表目前硬编码为字符串,但我想在数据库中查找CategoryName以验证它是否存在 显然,这样做的目的是确保用户不只是键入: www.domain.com/Categories/AnyUrlWillWork并返回有效页面 还有谁知道他们是如何处理资本化问题的,因为路由是区分大小写的?例如,类别/饮料是否应转发至类别/饮料 提前感谢您的帮助,很高兴加入Sta

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

这是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,所以这是全新的。顺便说一句:我认为你应该直接进入MVC。WebForms中的路由并不像现在那样自然,而且IMHO WebForms将随着MVC的出现而慢慢消亡。我正在考虑这个问题,但我需要掌握很多基础知识,我参加的培训/课程也没有涉及MVC。一旦我通过了,我会和你一起跳上马车。我编辑了我的答案,也为你提供了一种使用WebForms的方法,看看它是否有助于Hanks添加。这是有点超过我的头,但这是我跳进这个得到的。到目前为止,我一直在阅读本教程:并创建了一个DAL,它返回类别名称(categoriesAdapter.GetCategories();),但不确定如何继续并在这个类中使用它。但是,我可以绑定到页面上的控件。