Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Url.Action work Asp.net MVC?

This is somewhat related to another question I've asked but I figure why not ask it seperately.

If I were to place something like the following in a view

<td><img src='<%= Url.Action( "DisplayImage" , "User" , new { id = item.id} ) %>' alt="" /></td>

Is it supposed to display this?

<td>
   <img src='/User.mvc/DisplayImage?id=U00915441' alt="" />
</td>

Or would the value of the src-attribute actually be replaced with the results of the UserController GetImage Action?

like image 249
zSynopsis Avatar asked Nov 18 '09 21:11

zSynopsis


People also ask

What does URL action do?

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.

What is difference between HTML ActionLink and URL action?

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


1 Answers

It will construct the path to the action, returning a url, not the results of executing the action.

The results will be:

<td>
   <img src='/User.mvc/DisplayImage?id=U00915441' alt="" />
</td>

Example code. assumes your user model has the image stored in a byte array. If you are using LINQ and the property is a Binary, then use the ToArray() method to convert it to a byte array. Note the attributes which will require that the user be logged in and using a GET request.

[Authorize]
[AcceptVerbs( HttpVerbs.Get )]
public ActionResult DisplayImage( string id )
{
     var user = ...get user from database...

     return File( user.Image, "image/jpeg" );
}

}

like image 173
tvanfosson Avatar answered Sep 20 '22 15:09

tvanfosson