是否有更好的方法可以在不使用操作部分的情况下获得完全限定的控制器URL ?

时间:2022-10-10 20:24:44

I need the fully qualified URL of my controller for constructing URLs in Javascript. I do not, however want a specific action, since that will change depending on client side code. I tried (in my _Layout.cshtml)

我需要我的控制器的完全限定URL来构造Javascript中的URL。但是,我不需要特定的操作,因为这将根据客户端代码进行更改。我试过(在我的_Layout.cshtml)

Url.RouteUrl(new { controller = "Foo" })

However, this will invariably also give me the action for the current view, e.g. "/Foo/Bar". Of course, I could now throw away the last slash and everything after, but it seems to me there must be a better way to do this.

然而,这也必然会使我对当前观点采取行动。“/ Foo / Bar”。当然,我现在可以扔掉最后一个斜杠和之后的所有东西,但在我看来,一定有更好的方法可以做到这一点。

2 个解决方案

#1


1  

You can use:

您可以使用:

Request.Url.Host + Url.Action("Index", "Foo");

It'll automatically drop the action part when it generates the URL.

当它生成URL时,它将自动删除操作部分。

#2


0  

Found the solution myself. You can set the action name explicitly to an empty string:

我找到了解决办法。可以将操作名显式设置为空字符串:

Url.RouteUrl(new { controller = "Foo", action = "" })

#1


1  

You can use:

您可以使用:

Request.Url.Host + Url.Action("Index", "Foo");

It'll automatically drop the action part when it generates the URL.

当它生成URL时,它将自动删除操作部分。

#2


0  

Found the solution myself. You can set the action name explicitly to an empty string:

我找到了解决办法。可以将操作名显式设置为空字符串:

Url.RouteUrl(new { controller = "Foo", action = "" })