开发者

ASP.NET MVC Routing Help Required

I'm creating a online log viewer application which reads logs generated by many applications into a single common database. Log types are error, fatal, debug and I use all to represent all the logs.

I've a controller named AppsController which should server Views for the following requests, where "bi-reports" is one of the many application names we have.

/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

How should I configure routes set parameters in Action methods of Controller to get this working?


This is rough idea of your routing definition. I can see that you basically have three types of routes:

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

I've set year constraint to match years between 1900 and 2199, and months so they actually have to specify a valid month 01 to 12.

If you have any additional controllers you will have to define a default route as well and put controller constraint on this one or make controller name static (when just one applies).


I would do something along these lines

    ''# 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]*"})

Then your controller would have something like

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

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

        Return View()
    End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜