Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC @Url.Content vs @Url.Action

Tags:

asp.net-mvc

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

like image 485
Nate Pet Avatar asked Nov 18 '11 17:11

Nate Pet


People also ask

What is the use of URL action in MVC?

Action(String, String, Object, String)Generates a fully qualified URL to an action method by using the specified action name, controller name, route values, and protocol to use.

What is URL content?

A URL (Uniform Resource Locator) or webpage address, is a reference to a web resource that specifies its exact location online (within a computer network). A content URL refers to a webpage address that specifically relates to certain content on the internet, such as an image or a video file.

What is the difference between HTML ActionLink () and URL action ()?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.

What is a URL action?

A URL action is a hyperlink that points to a web page, file, or other web-based resource outside of Tableau. You can use URL actions to create an email or link to additional information about your data. To customize links based on your data, you can automatically enter field values as parameters in URLs.


2 Answers

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

like image 125
Richard Avatar answered Oct 09 '22 08:10

Richard


@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") 
like image 32
vcsjones Avatar answered Oct 09 '22 09:10

vcsjones