【问题标题】:asp.net basic routing not workingasp.net基本路由不起作用
【发布时间】:2016-04-15 13:25:03
【问题描述】:

我正在处理 asp.net 网络表单,但我遇到了一些路由问题,以下路由不起作用:

RouteTable.Routes.Add(new Route("{resource}.axd/{*pathInfo}", new StopRoutingHandler()));    
RouteTable.Routes.MapPageRoute("category", "en/Product/{ProductName}", "~/en/index.aspx");

我正在尝试的网址是: http://localhost:5562/en/Product.aspx?ProductName=Laptop

【问题讨论】:

  • 试试http://localhost:5562/en/Product/Laptop作为你的浏览器路由。
  • 感谢您的回复:它与您提到的网址一起使用,但我必须使用此网址:“localhost:5562/en/Product.aspx?ProductName=Laptop
  • 路由的目的是防止您需要使用您的 ASPX 页面 URL。路由映射到 index.aspx 而不是 product.aspx。因此,您创建一条新路线或修改现有路线,或者根本不使用路线。
  • 如果我必须防止某些产品名称出现在 url 中,那该怎么办?就像我喜欢不允许这个网址:localhost:5562/en/Product/computerlocalhost:5562/en/Product.aspx?ProductName=computer
  • 我已经发布了解决您的 cmets 的答案

标签: asp.net routing url-routing


【解决方案1】:

尝试http://localhost:5562/en/Product/Laptop 作为您的浏览器路由。

然后,根据您的 cmets,如果您想禁止一个值,请在读取该值的代码中执行此操作,在 index.aspx(或 product.aspx,如果您正在使用它)中:

string value = Page.RouteData.Values("ProductName"); // get the product being searched for from the URL
List<string> forbiddenValues = new List<string> { "Computer", "BadWord2", "BadWord3" }; // put your forbidden terms in here
if (forbiddenValues.Contains(s, StringComparer.CurrentCultureIgnoreCase)) // case-insensitive
{
    // Bad value detect - throw error or do something
    MyLiteral.Text = "Bad term found.  Cannot continue";
} else
{
    // do you database stuff here and get the products
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多