如何Url.Action工作Asp.net MVC? [英] How does Url.Action work Asp.net MVC?

查看:114
本文介绍了如何Url.Action工作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>

,还是有SRC属性实际上是由于UserController的getImage操作的结果被替换的价值?

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

推荐答案

这将构建路径行动,返回一个URL,执行操作的不结果。

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

结果将是:

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

举例code。假设你的用户模型存储在一个字节数组中的形象。如果您在使用LINQ和属性是二进制,然后用()将其转换为一个字节数组方法。请注意,这将要求用户在使用GET请求记录的属性。

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" );
}

}

这篇关于如何Url.Action工作Asp.net MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆