开发者

URL building in ASP.NET MVC

I have got four routes defined like this:

//Project lis开发者_Python百科t with page number
        routes.MapRoute(
            "Filtered projects 1",
            "Projects/All/{page}",
            new { controller = "Projects", action = "All", page = UrlParameter.Optional }
            );

        //Project list with page number and filtering by region
        routes.MapRoute(
            "Filtered projects 2",
            "Projects/All/Regions/{regions}/{page}",
            new { controller = "Projects", action = "All", regions = UrlParameter.Optional, page = UrlParameter.Optional}
            );

        //Project list with page number and filtering by subject
        routes.MapRoute(
            "Filtered projects 3",
            "Projects/All/Subjects/{subjects}/{page}",
            new { controller = "Projects", action = "All", subjects = UrlParameter.Optional, page = UrlParameter.Optional }
            );

        //Project list with page number and filtering by region and subject
        routes.MapRoute(
            "Filtered projects 4",
            "Projects/All/Regions/{regions}/Subjects/{subjects}/{page}",
            new { controller = "Projects", action = "All", regions = UrlParameter.Optional, subjects = UrlParameter.Optional, page = UrlParameter.Optional }
            );

This allows me to have a paged list which can also be filtered by Regions or Subjects.

I want to make a series of links with the subjects, such that I can click on a subject and it will return the one of the urls above as required, with the /Subjects/SubjectName bit appended as required. I also want to allow the user to click on several links, which should give urls like above with /Subjects/subject1_*subject2*_subject3.

I just can't figure out how to do this. I've got the list of subjects showing in a partial view. When I generate the hyperlinks, how can I get to the url and modify (or add or removed as required) the /Subjects/* part?

It was easy with querystrings, because they could easily be broken into value paris, but with these beautiful urls it seems quite difficult... Any tips?

UPDATE: I forgot to add that I would like to use this filtering method with different actions on the same controller. Not sure if that matters, but I thought I'd be precise.

UPDATE 2: I think my question isn't quite clear enough...

What I am trying to do is generate a link for each subject in my partial view with respect to the link from the main view. The catch is that the link needs to conform to one of the routes above.

Say, I call this from /localhost/Projects/All and want to sort by subject1, I would want to get urls saying /localhost/Projects/All/Subjects/subject1. If I called this from /localhost/Projects/Search/Something/10 (i.e. page 10 of the results for search "Something", I haven't got this route in the list, but it is in the works), I would want urls like localhost/Projects/Search/Something/Subjects/Subject1/10. Or for that matter going from /localhost/Projects/All/4 to localhost/Projects/All/Subjects/subject1/4.

I can easily append the /Subjects/Subject1 bit to the URL, but then doesn't conform to my routes anymore... In other words, how can I extract the various bits from my url, modify them and then stitch them back together?

Is this possible? Or should I just forget prettyness and use querystrings?


Use ViewContext.RouteData to obtain current route information.


Url.Action("All","Projects", new{subjects = "some subject"})

will give you the correct formulated url for one that just had a subject passed in, change the params in the anonymous object to alter it as a apporpriate

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜