4

in my Global.asax I have these rules

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

    }

I have also UrlRewrite to manage

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

    }

I tryied also to put this lines in 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" />

But all my pages with urlRewrite goes in PageNotFound.aspx

If the current Url is not contained in these rules,is there a way to Redirect the user to the Home Page? For exampla, if the current url is /CheckoutError how can I redirect the user to /Home?

Thanks

Martina
  • 1,852
  • 8
  • 41
  • 78
  • may be it's a bit hacky but have you tried to redirect from code behind of error page? In theory you can get the return url from the request query string, so you know where the user originally wanted to go. – derloopkat May 21 '18 at 19:56
  • @derloopkat I have to do instructions that now I do in Global.asax Application_BeginRequest method in page_Load of PageNotFound.aspx.cs? – Martina May 21 '18 at 19:58
  • nope, just handle what is going to error page and shouldn't – derloopkat May 21 '18 at 19:59

1 Answers1

0

Your question is duplicate of

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

You just need

RouteTable.Routes.MapPageRoute("defaultRoute", "{*value}", "~/Home.aspx");
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • Thanks, I added the question with UrlRewrite particular. – Martina May 21 '18 at 19:21
  • So basically it is a 404 as of now or what? – Tarun Lalwani May 21 '18 at 19:26
  • No, I am just trying to understand what you see on the UI as of now – Tarun Lalwani May 21 '18 at 19:39
  • if you go on address https://e-miko.com/abcd you have a 404 (and this is ok because I don't have a Rewrite with word 'abcd', so I could use rules in web.config. If I use these rules in web.config, also pages with Rewrite rule (stored in database table) for example https://www.e-miko.com/Scarpe-Da-Uomo goes in 404 error. – Martina May 21 '18 at 19:43
  • See this for event order and lifecycle https://stackoverflow.com/questions/46309071/rewriting-the-url-with-post-data-in-the-application-aquirerequeststate-event/47187763#47187763, i think you may need to hook a handler before HTTP handler and see if you work your way out from that – Tarun Lalwani May 22 '18 at 05:25