【问题标题】:How to generate a path/url from a Route in the Routes table?如何从 Routes 表中的 Route 生成路径/url?
【发布时间】:2010-11-25 14:40:33
【问题描述】:

我有一个 ASP.NET MVC Web 应用程序,并且在我的 Global.asax 中注册了许多路由。

我想知道如何在我的控制器中以编程方式构建(生成字符串 url)任何一个已注册的路由。

我在 .NET 4.0 的 Web 窗体中使用 Page.GetRouteUrl(routeName, routeParams) 做了同样的事情,但不知道如何在 MVC 中做同样的事情(我是 MVC 新手)。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-routing webforms


    【解决方案1】:

    您可以在控制器操作中使用 UrlHelper 类。

    public ActionResult Index()
    {
        string address = Url.RouteUrl(new { 
            action = "foo", controller = "bar", id = "123" 
        });
        // TODO: do something with the url
    
        return View();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-19
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      • 2012-05-06
      • 1970-01-01
      相关资源
      最近更新 更多