如何Url。行动Asp.net MVC工作吗?

时间:2022-11-30 21:08:46

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?

或者,src属性的值实际上会被UserController GetImage操作的结果所取代吗?

1 个解决方案

#1


9  

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

它将构造操作的路径,返回一个url,而不是执行操作的结果。

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.

示例代码。假设您的用户模型将映像存储在字节数组中。如果您正在使用LINQ,并且属性是二进制的,那么使用ToArray()方法将其转换为字节数组。注意,将要求用户登录并使用GET请求的属性。

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

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

}

}

#1


9  

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

它将构造操作的路径,返回一个url,而不是执行操作的结果。

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.

示例代码。假设您的用户模型将映像存储在字节数组中。如果您正在使用LINQ,并且属性是二进制的,那么使用ToArray()方法将其转换为字节数组。注意,将要求用户登录并使用GET请求的属性。

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

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

}

}