gpt4 book ai didi

c# - 从代码隐藏在 MVC 中生成 Url

转载 作者:太空狗 更新时间:2023-10-29 20:59:26 26 4
gpt4 key购买 nike

假设我在一个 Action 方法中,我想生成一个这样的字符串:

http://www.myhost.com/Home/Index?Id=1

我想将它保存到数据库中,所以我想知道是否有任何正式的方法来生成它而不是自己构建它。

我正在使用 MVC3

提前致谢。

最佳答案

您可以使用 Url Controller 的属性:

public ActionResult Foo()
{
string url = Url.Action("Index", "Home", new { id = 1 });
// TODO: save to DB
}

如果您需要一个绝对 url,只需使用适当的重载:

string url = Url.Action("Index", "Home", new { id = 1 }, "http");

关于c# - 从代码隐藏在 MVC 中生成 Url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8546144/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com