ASP.NET 路由 - 带数据库查找的路由约束
我刚刚开始使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 MVC 吗?如果是这样,为什么不路由到一个函数,该函数将根据您的数据存储检查类别名称,并在此类类别不存在时重定向到错误页面?
WebForms 解决方案是:
通过数据访问逻辑(使用 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?
A WebForms solution would be:
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.