Search code examples
asp.netc#-4.0asp.net-routing

ASP.net 4.0 Routing not working with main domain name


I have a simple routing for my website news section. Routing works fine for almost all URL's like

http://www.abc.com/Default.aspx

http://www.abc.com/Default.aspx?PageId=3

http://www.abc.com/Latest-News-Details.aspx?PageID=28&NewsID=39

Problem only happens when i try to access website with http://www.abc.com for some reason it redirects it to the custom error page, I am not able track error to source & i cant generate any such error on my localhost.

Code sample

global.asx file

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    RegisterRoutes(RouteTable.Routes);
    //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
}


public static void RegisterRoutes(RouteCollection routes)
{
    routes.Ignore("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" });
    routes.Ignore("{*allcss}", new { allcss = @".*\.css(/.*)?" });
    routes.Ignore("{*alljpg}", new { alljpg = @".*\.jpg(/.*)?" });
    routes.Ignore("{*alljs}", new { alljs = @".*\.js(/.*)?" });
    routes.Add(new System.Web.Routing.Route("{resource}.css/{*pathInfo}", new System.Web.Routing.StopRoutingHandler()));
    routes.Add(new System.Web.Routing.Route("{resource}.js/{*pathInfo}", new System.Web.Routing.StopRoutingHandler()));

    ////For News
    routes.MapPageRoute("NewsRoute", "{NewsID}/{PageID}/{NewsTitle}", "~/Latest-News-Details.aspx", false,
         new RouteValueDictionary {
                    { "NewsID", "0" },
                    { "PageID", "0"},
                    { "NewsTitle", "event-not-found" }},
         new RouteValueDictionary {   
                    { "NewsID", "[0-9]{1,8}" },
                    { "PageID", "[0-9]{1,8}" }
                }); 
}

Right now i have disabled routing for News Section still it generates error if i leave the above routes code un-commented .

I am not sure what is causing the problem.

how can i handle root for default domain like http://www.abc.com

I would appreciate help in this regards


Solution

  • Resolved issue by creating another route to handle http://www.abc.com

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.Ignore("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" });
        routes.Ignore("{*allcss}", new { allcss = @".*\.css(/.*)?" });
        routes.Ignore("{*alljpg}", new { alljpg = @".*\.jpg(/.*)?" });
        routes.Ignore("{*alljs}", new { alljs = @".*\.js(/.*)?" });
        routes.Add(new System.Web.Routing.Route("{resource}.css/{*pathInfo}", new System.Web.Routing.StopRoutingHandler()));
        routes.Add(new System.Web.Routing.Route("{resource}.js/{*pathInfo}", new System.Web.Routing.StopRoutingHandler()));
    
        routes.MapPageRoute(
             "HomeRoute",
             "",
             "~/Default.aspx"
         );
    
        ////For News
        routes.MapPageRoute("NewsRoute", "{NewsID}/{PageID}/{NewsTitle}", "~/Latest-News-Details.aspx", false,
             new RouteValueDictionary {
                        { "NewsID", "0" },
                        { "PageID", "0"},
                        { "NewsTitle", "News-not-found" }},
             new RouteValueDictionary {   
                        { "NewsID", "[0-9]{1,8}" },
                        { "PageID", "[0-9]{1,8}" }
                    }); 
    }
    

    It is strange for me as same routing working for other website with creating another rount to handle such request http://www.abc.com