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

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

问题描述

我有一个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)在Web窗体与.NET 4.0一样的东西,但无法弄清楚如何做同样的事情在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();
}

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

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