如何以编程方式获取MyController Show url?

时间:2012-02-08 16:33:48

标签: c# asp.net-mvc asp.net-mvc-2

(使用MVC 2)

从我的控制器操作中,我需要显示网址:

/MyController/Show/123

如何使用帮助程序生成此URL?

1 个答案:

答案 0 :(得分:1)

简单:

public ActionResult Show(int id )
{
    var completeURL = Request.RawUrl;
    var relativePath = Request.Path;
    var uriObject = Request.Url;
}   

来自其他控制器:

var url = Url.Action("Show", "MyController", new {id = 123});

但也许你只需要重定向?

public ActionResult Index()
{
    return RedirectToAction("Show", "MyController", new {id = 123});
}