如何从路由表中的路由生成路径/url? [英] How to generate a path/url from a Route in the Routes table?

查看:38
本文介绍了如何从路由表中的路由生成路径/url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an ASP.NET MVC web application, and I've registered a number of routes in my Global.asax.

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

I would like to know how I can programmatically build (generate a string url) any one of those registered routes from within my Controller.

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

I did the same thing in Web Forms with .NET 4.0 using Page.GetRouteUrl(routeName, routeParams) but can't figure out how to do the same thing in MVC (I'm an MVC newbie).

推荐答案

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

You could use the UrlHelper class inside your controller action.

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

    return View();
}

这篇关于如何从路由表中的路由生成路径/url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆