获取“默认”控制器中的路径网址 [英] Get "Default" Route Url in Controller

查看:93
本文介绍了获取“默认”控制器中的路径网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我检索默认路线的实际URL的语法是什么?

Can anyone tell me what is the syntax for retreiving the actual URL for the "Default" Route?

我想执行以下操作:

string url = RouteTable.Routes["Default"].ToString();
//(even though that code is completely wrong)

可使用的路由的url值。

so that I could have the url value of the route available to work with.

到目前为止,我一直在尝试 .GetVirtualPath()方法,但这仅返回路线数据

So far, I've been trying the .GetVirtualPath() method, but that only returns the route data for the current controller.

谢谢

戴夫

推荐答案

路由可以匹配任意数量的网址。因此,路线没有网址。要从路线获取网址,您必须向其提供所需网址的路线数据。为此,您只需对Url属性 Url.RouteUrl()使用 RouteUrl()方法。

A route can match any number of urls. So a route doesn't have a url. To get a url from a route you will have to supply it with the route data that you want the url for. To do that you simply use the RouteUrl() method on the Url property, Url.RouteUrl().

更新

如果您希望在提供路线时生成网址使用其默认值,您可以执行以下操作:

If you want the url that will be generated if you supply the route with its default values you can do something like this:

var url = Url.RouteUrl("Default", ((Route)RouteTable.Routes["Default"]).Defaults);

这篇关于获取“默认”控制器中的路径网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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