views:

686

answers:

2

In webforms, we would do somthing like this to set up a hander to generate a dyanmic image:

<img src="/barchart.aspx?width=1024&height=768&chartId=50" >

Then of course we would write code on the .aspx page to render the image using the parameters and write it back into the response.

I am honestly not sure how to set up/handle such a request with MVC and how we would activate it (in general terms) from a view.

any pointers or help in advance is greatly welcomed.

+3  A: 

If I understand the situation correctly:

public class ImageGeneratorController : Controller {
    public ActionResult BarChart(int width, int height, int chartId) {
        // ASP.NET MVC will map the request parameters to method arguments
    }
}

To create a link:

Url.Action("BarChart", "ImageGenerator", new {
    width = 1024,
    height = 768,
    chartId = 50
});

Will output:

/ImageGenerator/BarChart?width=1024&height=768&chartId=50
David Brown
this is what I wanted. What does the URL look like to map to this ?
MikeJ
/BarChart/1024/768/50 ?
MikeJ
I updated my answer to show URL generation and the result
David Brown
@MikeJ -- not unless you use something other than default routes.
tvanfosson
Thanks David. Thats really, really helpful to me. I think thats one of the confusing things about MVC for n00bs like me. Is how to map "old" urls to the /controller/action/parameter method, but I can see that there is a way that this doesnt have to be done this way. nice.
MikeJ
+2  A: 

You don't need a view to achieve this. You can have an action that returns a FileResult and write the image to the response like this :

public FileResult BarChart(int width, int height, int chartID) {
    //create the chart
    return new FileContentResult(byte[] fileContents, string contentType);
}

And the html :

<img src="/yourController/BarChart/1024/768/50">
çağdaş
Wouldn't that specific URL require a custom named-route?
David Brown
Yes. At least for the parameters to look that way.
çağdaş