asp.net-mvc MVC @Url.Content 与 @Url.Action

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8186163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:35:49  来源:igfitidea点击:

MVC @Url.Content vs @Url.Action

asp.net-mvc

提问by Nate Pet

I have looked online but was unable to find the difference between using @Url.Contentvs @Url.Action.

我在网上查看过,但无法找到使用@Url.Contentvs之间的区别 @Url.Action

回答by Richard

Url.Contentis used when you wish to resolve a URL for any file or resource on your site and you would pass it the relative path:

Url.Content当您希望解析站点上任何文件或资源的 URL 时使用,并且您将向它传递相对路径:

@Url.Content("~/path/file.htm")

Url.Actionis used to resolve an action from a controller such as:

Url.Action用于解析来自控制器的操作,例如:

@Url.Action("ActionName", "ControllerName", new { variable = value })

See here for more info:

请参阅此处了解更多信息:

http://geekswithblogs.net/liammclennan/archive/2008/05/21/122298.aspx

http://geekswithblogs.net/liammclennan/archive/2008/05/21/122298.aspx

回答by vcsjones

@Url.Actionis used to create a URL to an Action in a controller. For example, assuming you had a controller that looked like this:

@Url.Action用于创建控制器中操作的 URL。例如,假设您有一个如下所示的控制器:

public YourControllerController : Controller
{
    public ActionResult YourAction() { /* stuff */ }
}

You could create a URL that invokes the action with it like this:

您可以创建一个 URL 来调用操作,如下所示:

Url.Action("YourAction", "YourController")

@Url.Contentresolves a virtual path into an absolute path. Example:

@Url.Content将虚拟路径解析为绝对路径。例子:

Url.Content("~/images/image.jpg")