ASP.NET MVC: Route to URL

天涯浪子 提交于 2019-11-30 22:40:53

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<string> urlList = new List<string>();
        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.

How about this (in the controller):

    public IEnumerable<SiteMapEntry> SiteMapEntries
    {
        get
        {
            var entries = new List<SiteMapEntry>();

            foreach (var route in this.Routes)
            {
                entries.Add(new SiteMapEntry
                (
                    this.Url.RouteUrl(route.Defaults),
                    SiteMapEntry.ChangeFrequency.Weekly,
                    DateTime.Now,
                    1F));
            }

            return entries;
        }
    }

Where the controller has member:

public IEnumerable<Route> Routes

Take note of:

this.Url.RouteUrl(route.Defaults)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!