1

这是我显示图像的目标页面设计

<img alt="" src="@Url.Action("GetPhoto", "Display")" height="50" width="50" class="photo" />

这是我的源页面,我将我的绑定linkbutton如下

@Html.ActionLink("QuestionTitle", "Index", "Display", new { QuestionID = item.QuestionID }, null)

我在浏览器中单击上述网址时的目的地如下

http://localhost:1931/Display?QuestionID=1

这是我试图显示图像的代码

[HttpGet]
public ActionResult GetPhoto()
{
    // int quesID = Convert.ToInt16(Request.QueryString["QuestionID"].ToString()); this didn't worked so by having a look at quick watch I write the below
    int quesID = Convert.ToInt16(Request.UrlReferrer.Query.Substring(12, 1));
        byte[] photo = null;

        var usrname = (from a in db.tblQuestions
                       where a.QuestionID == quesID
                       select new { a.UserName });
        var v = db.tblUsers.Where(p => p.UserName == usrname.FirstOrDefault().UserName).Select(img => img.Photo).FirstOrDefault();
        photo = v;
        return File(photo, "image/jpeg");
    }

有人可以告诉我如何在querystring除此页面之外的每个页面上获取查询字符串。

4

1 回答 1

2

在您的目标页面中,您应该使用

<img alt="" src="@Url.Action("GetPhoto", "Display", new {QuestionId=Model.Id})" height="50" width="50" class="photo" />

(例如,我假设您有一个带有 Id 属性的模型)。

这与您在源页面中调用显示控制器的索引操作的方式相同

没有 Route 数据@Url.Action("GetPhoto", "Display")生成链接http://localhost:1931/Display/GetPhoto

使用路由数据(第三个参数)生成的链接是http://localhost:1931/Display/GetPhoto?QuestionId=<some value>

于 2013-02-11T13:59:42.473 回答