Image routing relative to views in Asp.Net MVC?

I need to be able to place images along a path relative to the view, but I'm not sure how to do this because I'm not very good at MVC routing:

If I have a route like this:

routes.MapRoute(
                "Parameter",
                "{controller}/{action}/{lang}/{prod}",
                new { controller = "Manuals", action = "Product", lang = "en-US", prod = "productname" }

      

And then I try to use the relative path for the images:

<embed class="images src="@Url.Content("images/sampleimage.svg")"></embed>

      

This gives me a path (if I check it in the web inspector) that looks like src = "HTTP: //localhost/Manuals/Product/en-US/images/sampleimage.svg"

where there is no image. It is physically located at http: //localhost/Views/Manuals/en-US/productname/images/sampleimage.svg

Basically, the Views folder and then no folder like the action name (Product). So how can I use paths relative to the view in this environment? The action becomes part of the url, but there is no actual folder named as the action method, and also the images will be under the Views folder which is not part of the url here ... Also, the last part of the url in the route (product name ) doesn't seem to show up at all in the image path, it seems to be interpreted as an id or something because I'm only using one action method to serve views based on lang and prod parameters?

Isn't there a way to place images like this and use relative paths like "images / imagename.svg" or the like? Or am I just misunderstanding routing in MVC?

I need to do it this way to make it easier to add a large array and result in XSL transformations ...

EDIT: Sorry, I had some wrong urls regarding the results I got. Should be fixed now to reflect what I have.

+3


source to share


2 answers


I'm not sure if you understand how the infrastructure is supposed to work.

My first thought is about convention. The convention is that you save your views in the Views folder. Then you can use DisplayTemplates, EditTemplates and Shared for your display / edit templates or shared partial views. This is the convention that MVC developers use, so if you start putting something else in these folders, you're going to confuse other developers and eventually yourself.



What are you trying to achieve? Are you following URLs in a specific format? One option you have is to implement an action that will return your image. Other alternatives are URL rewriting or routing to static files: Using ASP.NET Routing to Serve Static Files

+2


source


Use controller action to get images from DB or disk. Below is an example of getting images from the "ImageFolder" folder in the site root.

public virtual FilePathResult GetImage(string imageId){
    var path = Path.Combine(Server.MapPath("~"), "ImageFolder", imageId + ".svg");
    return new FilePathResult(path, "image/jpg");
}

      

You can use something like this to get sample images for your products.



In ASP.NET MVC, you don't rely on a folder hierarchy to navigate your site. There are several folder conventions in the framework, but they are not used for routing.

For static images, you can place them in the Content / images folder.

+2


source







All Articles