ASP.NET MVC: Route to URL

前端 未结 2 573
無奈伤痛
無奈伤痛 2021-01-06 05:22

What\'s the easiest way to get the URL (relative or absolute) to a Route in MVC? I saw this code here on SO but it seems a little verbose and doesn\'t enumerate the RouteTab

2条回答
  •  半阙折子戏
    2021-01-06 06:27

    Use the UrlHelper class: http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.aspx

    You should be able to use it via the Url object in your controller. To map to an action, use the Action method: Url.Action("actionName","controllerName");. A full list of overloads for the Action method is here: http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.action.aspx

    so your code would look like this:

            List urlList = new List();
            urlList.Add(Url.Action("Edit", "Help"));
            urlList.Add(Url.Action("Create", "Help"));
            urlList.Add(Url.Action("Company", "About"));
            urlList.Add(Url.Action("Management", "About"));
    

    EDIT: It seems, from your new answer, that your trying to build a sitemap.

    Have a look at this Codeplex project: http://mvcsitemap.codeplex.com/. I haven't used it myself, but it looks pretty solid.

提交回复
热议问题