2

I'm trying to create an Asp.Net MVC 3 application where one Action method in a Controller is to handle many different requests and be able to serve a View with product information, and where the product is specified in an id parameter.

The reason is that users (non-programmers) want to be able to easily add new Views, without having to add action methods and so on.

Each View will have image elements, with src attributes referring to a relative path (e.g. "images/image_1.svg")

Now, the only way I could come up with to do this was to have an action method that looks like this:

//This takes a url with the format /Manuals/Product/id_sample, where the last is an id parameter
    public ActionResult Product(string id)
    {
        return View(id + "/" + id);           
    }

The reason I concatenate the path to the view with the id twice is to be able to have a separate folder for each product, with its own "images" folder inside.

So the views are in a folder structure that looks like this:

Views
     Manuals
            id_sample (this folder containing a view also called id_sample.cshtml)
                     images

This does serve the correct view given a path like above. However, I realized that this is probably not a good way to do it. It produces a url that does not work with the relative paths of the images.

I get a "resource cannot be found" for the images, and in the web inspector tool I can see that it is looking for the image in the path localhost/Manuals/Product/images/image_1.svg, which of course does not exist.

So how can I do this and be able to use simple relative paths for the images in the Views?

Please note that the Views are created using XSLT transforms from XML, so all the image paths are created there.

If there is a completely different way of doing this, please tell me, I have a feeling I'm going about this the wrong way. And if the solution has to do with setting up routing, please be very detailed in the explanation, because I don't have a very good grasp of the routing mechanism of Asp.Net MVC...

Pops
  • 30,199
  • 37
  • 136
  • 151
Anders
  • 12,556
  • 24
  • 104
  • 151

0 Answers0