需要ASP.NET MVC路由帮助

时间:2011-07-11 16:27:13

标签: asp.net-mvc asp.net-mvc-2 url-routing

我正在创建一个在线日志查看器应用程序,它将许多应用程序生成的日志读入单个公共数据库。日志类型是错误,致命,调试,我使用all来表示所有日志。

我有一个名为AppsController的控制器,它应该为以下请求提供服务器视图,其中“bi-reports”是我们拥有的众多应用程序名称之一。

/apps/bi-reports/
/apps/bi-reports/all
/apps/bi-reports/error/
/apps/bi-reports/2011/04/
/apps/bi-reports/2011/04/all
/apps/bi-reports/2011/error
/apps/bi-reports/2011/04/error
/apps/bi-reports/all/last-hundred
/apps/bi-reports/all/most-hundred
/apps/bi-reports/2011/last-hundred
/apps/bi-reports/2011/04/all/last-hundred

如何在Controller的Action方法中配置路由设置参数以使其正常工作?

2 个答案:

答案 0 :(得分:1)

这是您的路由定义的粗略概念。我可以看到你基本上有三种类型的路线:

routes.MapRoute(
    "IrrelevantDates",
    "{controller}/{application}/{type}/{range}",
    // defaults
    new {
        controller = "Apps",
        action = "UnboundReport",
        type = "all",
        range = "no-limit"
    },
    // constraints
    new {
        type = "apps|error"
    }
);

routes.MapRoute(
    "RelevantYearOnly",
    "{controller}/{application}/{year}/{type}/{range}",
    // defaults
    new {
        controller = "Apps",
        action = "YearlyReport",
        type = "all",
        range = "no-limit"
    },
    // constraints
    new {
        year = "19\d{2}|2[01]\d{2}",
        type = "apps|error"
    }
);

routes.MapRoute(
    "RelevantYearAndMonth",
    "{controller}/{application}/{year}/{month}/{type}/{range}",
    // defaults
    new {
        controller = "Apps",
        action = "MonthlyReport",
        type = "all",
        range = "no-limit"
    },
    // constraints
    new {
        year = "19\d{2}|2[01]\d{2}",
        month = "0[1-9]|1[0-2]",
        type = "apps|error"
    }
);

我设置年份约束以匹配19002199之间的年份,以及几个月,因此他们实际上必须指定有效的月0112

如果您有任何其他控制器,您还必须定义一个默认路由并将控制器约束放在此控制器上,或者使控制器名称为静态(只有一个适用)。

答案 1 :(得分:0)

我会沿着这些方向做点什么

    ''# note, this is untested VB and might need some tweaking.
    routes.MapRouteLowercase("Base", "",
                             New With {.controller = "Home",
                                       .action = "Index",
                                       .year = UrlParameter.Optional,
                                       .paging = UrlParameter.Optional},
                             New With {.year = "[0-9]*"})

然后你的控制器会有类似

的东西
    Function Index(ByVal paging As String, ByVal year As Integer?) As ActionResult

        ''# do your pre-processing for paging and year.

        Return View()
    End Function