【问题标题】:Know if current url is contained in RouteTable.Routes知道当前 url 是否包含在 RouteTable.Routes 中
【发布时间】:2018-10-29 19:52:15
【问题描述】:

在我的 Global.asax 我有这些规则

    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.EnableFriendlyUrls();
        RouteTable.Routes.MapPageRoute("", "Home", "~/Default.aspx");
        RouteTable.Routes.MapPageRoute("", "Carrello", "~/Carrello.aspx");
        RouteTable.Routes.MapPageRoute("", "Checkout", "~/Checkout2.aspx");
        RouteTable.Routes.MapPageRoute("", "Ricerca-Prodotto/{Filtri}/{Pagina}", "~/ProductsSearch.aspx");
        RouteTable.Routes.MapPageRoute("", "Prodotto/{ProductId}", "~/Product.aspx");
        RouteTable.Routes.MapPageRoute("", "Prodotti/{Menu}/{Marca}/{Categoria}/{Pagina}", "~/Product.aspx");

    }

我也有 UrlRewrite 来管理

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        System.Collections.Generic.List<Rewrite> rewrites = Rewrite.getRules();
        String fullOriginalPath = Request.Url.ToString();
        int index = 0;
        if(fullOriginalPath.Contains("www."))
            index = fullOriginalPath.IndexOf('/', fullOriginalPath.IndexOf("e-miko.com")) + 1;
        else
            index = fullOriginalPath.IndexOf('/', fullOriginalPath.IndexOf("localhost")) + 1;

        string chiave = fullOriginalPath.Substring(index);

        Utility.WriteToLog(chiave);

        foreach (Rewrite r in rewrites)
        {
            if(r.Chiave == chiave)
            {
                string url = "/" + r.Pagina;
                if (r.Param1 != null)
                    url += "/" + r.Param1;
                if (r.Param2 != null)
                    url += "/" + r.Param2;
                if (r.Param3 != null)
                    url += "/" + r.Param3;
                if (r.Param4 != null)
                    url += "/" + r.Param4;
                if (r.Param5 != null)
                    url += "/" + r.Param5;
                Context.RewritePath(url);
            }
        }

    }

我也尝试将这些行放在 web.config 中

<httpErrors errorMode="Custom" existingResponse="Replace">
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" path="/PageNotFound.aspx" responseMode="ExecuteURL" />
    <remove statusCode="500" subStatusCode="-1" />
    <error statusCode="500" path="/PageNotFound.aspx" responseMode="ExecuteURL" />

但是我所有带有 urlRewrite 的页面都在 PageNotFound.aspx 中

如果当前的 Url 不包含在这些规则中,有没有办法将用户重定向到主页? 例如,如果当前 url 是 /CheckoutError 我如何将用户重定向到 /Home?

谢谢

【问题讨论】:

  • 可能有点hacky,但您是否尝试过从错误页面后面的代码重定向?理论上你可以从请求查询字符串中得到返回url,这样你就知道用户最初想去哪里了。
  • @derloopkat 我必须执行现在我在 PageNotFound.aspx.cs 的 page_Load 中的 Global.asax Application_BeginRequest 方法中执行的指令?
  • 不,只处理错误页面的内容,不应该

标签: asp.net webforms friendly-url


【解决方案1】:

您的问题与

重复

asp.net 4.0 web forms routing - default/wildcard route

你只需要

RouteTable.Routes.MapPageRoute("defaultRoute", "{*value}", "~/Home.aspx");

【讨论】:

  • 谢谢,我特别添加了 UrlRewrite 的问题。
  • 所以现在基本上是 404 还是什么?
  • 不,我只是想了解您目前在 UI 上看到的内容
  • 如果你继续地址e-miko.com/abcd你有一个404(这没关系,因为我没有用单词'abcd'重写,所以我可以在web.config中使用规则。如果我在 web.config 中使用这些规则,以及带有重写规则(存储在数据库表中)的页面,例如 e-miko.com/Scarpe-Da-Uomo 出现 404 错误。
  • 查看此事件顺序和生命周期stackoverflow.com/questions/46309071/…,我认为您可能需要在 HTTP 处理程序之前挂钩一个处理程序,看看您是否能解决这个问题
猜你喜欢
  • 2018-02-18
  • 1970-01-01
  • 1970-01-01
  • 2012-06-12
  • 1970-01-01
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多