Andrei
Andrei

Reputation: 4237

Passing an url as parameter to controller action

On my asp.net mvc page I need to render multiple images that are stored on third-party images hosting service. The service has some api which returns me a list of image urls. In *.cshtml file a have the fallowing html markup:

@foreach (var img in Model.Images)
{
   <img src="@img.ImageUrl" />
}

It's works perfectly. But for some reason I can't use direct URL to the image in the "src" attribute. Next, I have created new async controller that should return an image:

public class ImagesController : AsyncController
    {
        //
        // GET: /Images/
        public void GetImageAsync(string url)
        {
            AsyncManager.OutstandingOperations.Increment();
            WebRequest request = WebRequest.Create(url);
            WebResponse response = request.GetResponse();
            AsyncManager.Parameters["response"] = response;
            AsyncManager.OutstandingOperations.Decrement();
        }

        public FileResult GetImageCompleted(WebResponse response)
        {
            return base.File(response.GetResponseStream(), response.ContentType);
        }

    }

Now I need to pass a full image url to my controller action.

Something like this:

@foreach (var img in Model.Images)
    {
       <img src="Images/GetImage/**@img.ImageUrl**" />
    }

how to create a new route for passing this parameter to action?

Thanks!

Upvotes: 0

Views: 2565

Answers (1)

rsenna
rsenna

Reputation: 11992

You mean this?

routes.MapRoute(
    "GetImage",
    "Images/GetImage/{*url}",
    new { controller = "Images", action = "GetImageAsync" }
);

I'm not sure why you need all that though. An AsyncController + routing in order to fetch all image url's seems to be overkill...

Upvotes: 1

Related Questions