0

First and foremost I am new to MVC, so I have a learning curve.

After reading the following post - Display image from database in asp mvc

I have included in the _Layout.cshtml the following markup and razor per the post above

<img src="@Url.Action("show", "image", new { imageName = Session["WebSite.LogoFileName"]})" />

The shared view _Layout.cshtml is calling the HomeController which has

[AllowAnonymous]
public ActionResult Index()
{
    return View();
}

From what I understand in the post I included the following in the HomeController as well.

[AllowAnonymous]
public ActionResult Show()
{
    return View();
}

The image controller has the following

// GET: /Image/
public ActionResult Show(string imageName)
{
    var imageData = ReadBytesFromDatabase(Session["WebSite.LogoFileName"].ToString());

    return File(imageData, "image/jpg");  //I have excluded the File method on purpose.
}
Community
  • 1
  • 1
Moojjoo
  • 729
  • 1
  • 12
  • 33
  • 3
    Why do you need to call `ImageController` from `HomeController`? This syntax will call `Show` method from `ImageController`: `` – ekad Sep 26 '14 at 12:35
  • I feel your pain, MVC is a difficult learning curve. – Luke Sep 26 '14 at 12:36
  • Ok, so why is the ImageController not being called? Again, please understand the learning level... – Moojjoo Sep 26 '14 at 12:42
  • I think you need to explain what your purpose is, are you trying to get the imageData? or something else? – ekad Sep 26 '14 at 12:46
  • I am streaming Images from a MemoryStream by passing the imageName to the Controller... The File(imageData, "image/jpg") is a helper method that will ReadBytesFromDatabase from a SQL Server FileStream by passing the LogoFileName. – Moojjoo Sep 26 '14 at 12:54
  • You can try to put a break point inside the `Show` method in `ImageController` and then run your website. When you load the home page, the break point will be hit. That's when the `Show` method is actually called. – ekad Sep 26 '14 at 13:33
  • Unfortunately, it is not hitting the break point. Any suggestions? – Moojjoo Sep 26 '14 at 13:42
  • Does the view you're opening use `_Layout.cshtml`? – ekad Sep 26 '14 at 13:43
  • Yes it does use layouts. – Moojjoo Sep 26 '14 at 13:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61994/discussion-between-ekad-and-moojjoo). – ekad Sep 26 '14 at 13:46
  • The issue was there is authentication on the site and the attibute of [AllowAnonymous] was not on the controller so it was not getting hit. – Moojjoo Sep 26 '14 at 13:54
  • I see, so is the problem solved by adding `[AllowAnonymous]` attribute in the `Show` method? – ekad Sep 26 '14 at 13:56

1 Answers1

1

I think you have mistaken Url.Action For Html.Action.

Url.Action creates the link to the action. So in this case it will generate something like:

/Images/Show/ImageName.png

But you want the result of the action. So Changing Url.Action to Html.Action should do the trick.

Jamie
  • 3,031
  • 5
  • 36
  • 59
  • Url.Action("Show"), "Image", new { imageName = Session["WebSite.LogoFileName"]}" is correct and working. Thank you. – Moojjoo Sep 26 '14 at 17:22