【问题标题】:ActionResult not being called未调用 ActionResult
【发布时间】:2015-09-28 13:09:13
【问题描述】:

我有一个视图,我正在调用一个 ActionResult 方法,但在该方法中放置一个断点告诉我它没有被调用。

<div>
<ul class="list-group">
    @foreach (var item in Model)
    {
        <li class="list-group-item">
            <h4>Slide ID: @item.SlideId</h4>
            <p><i>Received: @item.TimeStamp</i></p>
            <div class="row">
                <div class="col-md-4">
                    <h4>@Html.ActionLink("View details", "Well", new {slideid = item.SlideId})</h4>
                    <img src="@Url.Action("Index", "Images", new {id = item.SlideId})"/> //This is where I want to call the method                       
                </div>
            </div>
        </li>
    }
</ul>

方法如下:

public class ImagesController : Controller
{
    // GET: Images
    public ActionResult Index(string id)
    {
        byte[] imageData = new byte[0];
        string cs = "Data Source=" + "some path";

        using (SQLiteConnection con = new SQLiteConnection(cs))
        {
            string stm = "SELECT LastImage FROM Well WHERE SlideId = " + "'" + id + "'";
            con.Open();

            using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
            {
                using (SQLiteDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        imageData = Serialize(rdr["LastImage"]);
                    }

                    rdr.Close();

                }
            }

            con.Close();
        }
        return File(imageData, "image/png");
    }

    public static byte[] Serialize(object obj)
    {
        var binaryFormatter = new BinaryFormatter();
        var ms = new MemoryStream();
        binaryFormatter.Serialize(ms, obj);
        return ms.ToArray();
    }
}

我试图用这段代码实现的是将图像从数据库加载到视图中。关于我做错了什么的任何提示?

现在使用 RouteConfig:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

【问题讨论】:

  • 转到图片网址时看到了什么?
  • Id 应该是整数。如果您需要它是一个字符串,请给参数另一个名称。但是“Id”应该是整数
  • @jrummell 然后我得到一个 404。ActionResult Index() 永远不会被调用。
  • @Khaine775 只需检查您的 RouteConfig.cs 这也很有意义...
  • 这段代码很容易受到sql注入。

标签: asp.net asp.net-mvc


【解决方案1】:

当您编写&lt;img src="@Url.Action("Index", "Images", new {id = item.SlideId})"/&gt; 时,您不会调用操作,而是调用路由 url。结果是一个字符串,例如localhost:8080/images/index/abcd123456,所以,如果你想调用动作,你需要使用@Html.Action("Index", "Images", new {id = item.SlideId})。注意@Html.Action 而不是@Url.Action

【讨论】:

  • 谢谢,现在该方法肯定被调用了,但是我得到一个“使用自定义 TextWriter 时,OutputStream 不可用。”。
  • 如果要显示图片,不要调用Html.Action
【解决方案2】:

我认为与其为每个图像打开和关闭数据库连接,更好的方法是收集所有信息以呈现该页面并将其发送到您发布的视图模型中。假设它称为 HomeController 的 Index 动作。它看起来像这样:

public class HomeController : Controller
{
    public ActionResult Index(string id)
    {
        var listOfItems = new List<SomeClass>();

        string cs = "Data Source=" + "some path";
        using (SQLiteConnection con = new SQLiteConnection(cs))
        {
            string stm = "SELECT SlideId, TimeStamp, LastImage FROM Well"; 
            con.Open();

            using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
            {
                using (SQLiteDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        var someItem = new SomeClass()
                        {
                            SlideId = rdr["SlideId"],
                            ImageData = Serialize(rdr["LastImage"]),
                            TimeStamp = rdr["TimeStamp"]
                        };
                        listOfItems.Add(someItem);
                    }

                    rdr.Close();
                }
            }

            con.Close();
        }

        return View(listOfItems);
    }
}

当然,如果项目太多,您应该始终考虑分页并限制列表中的项目数量以缩短响应时间。

【讨论】:

  • 您对 this 将图像添加为 base64 字符串有何看法?如果我在你的回答中做了像你一样的事情,除了将其转换为 base64 字符串之外,还有其他方法可以渲染图像吗?但它对我不起作用,因为它不渲染任何东西。
  • @Khaine775:实际上,如果我正在实施这个项目,我会将图像存储在 AWS S3 上并发送图像的 URL 而不是数据本身。这样你就可以使用像 AWS CloudFront 或类似的 CDN。如果您要发送图像数据,我认为我知道的最好方法是在 Base64 中编码/解码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-13
  • 2012-08-16
  • 2020-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多