ASP.net 4.0路由不使用主域名

时间:2012-11-25 10:11:20

标签: asp.net c#-4.0 asp.net-routing

我的网站新闻部分有一个简单的路由。路由适用于几乎所有URL,如

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

问题只发生在我尝试使用 http://www.abc.com 访问网站时出于某种原因将其重定向到自定义错误页面,我无法跟踪错误来源和放大器;我无法在本地主机上生成任何此类错误。

代码示例

global.asx文件

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

现在我已禁用新闻部分的路由,如果我将上述路线代码取消注释,它仍然会产生错误。

我不确定导致问题的原因。

如何处理默认域的root,如http://www.abc.com

我很感激这方面的帮助

1 个答案:

答案 0 :(得分:0)

通过创建另一个处理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}" }
                }); 
}

对我来说很奇怪,因为为其他网站工作的相同路由创建另一个rount来处理此类请求http://www.abc.com