MVC @Url.Content vs @Url.Action

asp.net Mvc

asp.net Mvc Problem Overview


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

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

Url.Content is 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("~/path/file.htm")

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

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

See here for more info:

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

Solution 2 - asp.net Mvc

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

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

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

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

@Url.Content resolves a virtual path into an absolute path. Example:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNate PetView Question on Stackoverflow
Solution 1 - asp.net MvcRichardView Answer on Stackoverflow
Solution 2 - asp.net MvcvcsjonesView Answer on Stackoverflow