Routing images relative to views in Asp.Net MVC? - asp.net-mvc-3

I need to be able to place images in a path relative to the view, but I'm not sure how to accomplish this, because I don't have a very good grasp of 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 a relative path for the images:
<embed class="images src="#Url.Content("images/sampleimage.svg")"></embed>
This gives me a path (if I inspect it in the web inspector) that looks like this:
src="http://localhost/Manuals/Product/en-US/images/sampleimage.svg"
which is not where the image is. It is located physically at "http://localhost/Views/Manuals/en-US/productname/images/sampleimage.svg"
So 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 like 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 (productname) does not seem to show up at all in the image path, it seems to be interpreted as an id or something because I use only one action method to serve the views based on parameters lang and prod?
Is there no way to place images like this, and use relative paths like "images/imagename.svg" or the like? Or am I just misunderstanding the routing in MVC?
I need to do it like this to simplify adding content which is massive and the result of XSL transformations...
EDIT: Sorry, I had some of the urls incorrect as to the results I got. Should be fixed now to reflect what I have.

I'm not sure whether you understand how the framework is supposed to work.
My first point is regarding the conventions. The convention is that you store your views in the Views folder. You may then have DisplayTemplates, EditTemplates and Shared for your display/edit templates or shared partial views. This is a convention that MVC developers are used to, so if you start placing something else in these folders, you are going to confuse other developers, and eventually yourself.
What are you trying to achieve? Are you after URLs in some specific format? One of the options that you have, is to implement action that will return your image. Another alternatives are URL re-writing or routing to static files: Using ASP.NET routing to serve static files

Use a controller action to get the images from DB or disk.
Here is an example of getting images from a folder called "ImageFolder" 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 could use something like that to retrieve the sample images for your products.
In ASP.NET MVC you don't rely in folder hierarchy to navigate your site. The framework has some folder convention, but they are not used for routing.
For static images, you can place them in the Content/images folder.

Related

Codeigniter - Is it possible to redefine anchor function

I have human friendly urls without index.php. I had to modify .htaccess file for that. Actually I always use Codeigniter like this. My url-s always look like this:
www.example.com/controller/function/parameter
So if I have an extra url parameter, then the url looks like this:
www.example.com/controller/function/parameter?archive=2013
Now what I want to do: If there is 'archive' parameter in the url, than also add that to the url when anchor function creates a link.
We have some different stuff every year (like stylesheets), so I need to make this navigation automatic. Am I thinking in the right direction?
And the answer is "Yes".
I slightly modified the solution from here:
How to extend anchor() function to anchor_admin() in CodeIgniter?

EmberJS will not load images when paths are stored as model attributes

I have a simple EmberJS app. There are a bunch of images stored in app/images. I can load an image from this folder in any template if I hardcode a path: <img src="images/MYIMAGE.png"/>. These images are associated with records though, and I have paths to them stored as attributes of the records. The path is a computed property from the filename persistent attribute. When I attempt to load the image through an Emblem template with img src="{{path}}", the image does not load. Ember is trying to fetch the image from this url:
http://localhost:9000/images/%3Cscript%20id='metamorph-71-start'%20type='text/x-placeholder'%3E%3C/script%3E%3Cscript%20id='metamorph-71-end'%20type='text/x-placeholder'%3E%3C/script%3E
If it makes a difference, I scaffolded the app with Yeoman and I have Grunt running a ton of preprocessors on my code (the standard ones that come with the Yeoman Ember generator). I thought maybe the images were being renamed, but the fact that I can retrieve them with a hardcoded path suggests otherwise. What's going on?
You need to use {{bind-attr}} for dynamic attributes like that.
<img {{bind-attr src=path}}>
See http://emberjs.com/guides/templates/binding-element-attributes/

PyroCMS: How Do I Create a multi Segment URL ?

I want my url to be like this:
mysite.com/categories/roofing
I can achieve this by creating a new navigation group called categories and make roofing a child BUT I do not want a new Navigation group. I want to be able to call a specific function by identifying segment 1 while still having the url include segment 2.
I tried doing it in "Page Types" but it will not allow you to create a multi segment slug.
I have the stream module if that helps.
Many Thanks !!
This is actually really simple and there are a few ways to do it. The simple way would be to create a page with the slug categories (you can redirect this page using routing later if you don't want it to exist), and then create a page with the slug roofing. Finally, in the pages section of the admin, drag roofing under categories as a child.
This can also be done using routing. You would create a routes.php file with the config folder with the following:
$route['categories/(:any)'] = '$1';

Magento: Rewriting URLs dynamically depending on conditions?

I have this problem:
I have a default route that looks like this: .../myapp/index.php/config/configuration/test/product_id/51
As you see, this leads to the ConfigurationController and in there to the testAction function. Thats fine and it should stay this way. What I want to change though is the "config part" of the URL.
So instead of
http://somedomain.de/myapp/index.php/config/configuration/test/product_id/51
I want
http://somedomain.de/myapp/index.php/furniture/configuration/test/product_id/51
to lead to the same location (ConfigurationController and in there to the testAction function). The part that is changed, is not static, its the attribute set name of the product. So instead of furniture it can be computer or something else. I can already get this from the product, So all I need is the rewrite stuff.
Ideas how to do that? THANKS!
In the module's config.xml you can change the frontend/routers/your_router/args/frontName which will allow you to use a different name (like furniture). If its an adminhtml controller, you have to use an adminhtml router but the rest is the same.
You can use a router, just like Magento does for CMS pages (explained in the second part of this article : http://alanstorm.com/magento_dispatch_admin_cms_default_routers)

Orchard CMS Render module view in homepage

I'm trying to render a view, defined in a module, in the main site homepage (~/) as it's main content. If the user is not authenticated, i need to show a login/register view instead.
The logged-in view lives in one module (Product Module) and the login/register view lives in another (Account Module). The logged-in view requires a service call to fetch data based on the user's products. I'm currently using standard mvc to render these views and fetch the data they require in their controllers.
Can this be accomplished by treating these views as shape templates? If so, are there any examples of pulling in views to the homepage like this? Or is there a better way of achieving this?
I have tried implmenting IHomePageProvider to return my own homepage ViewResult within the Product module, but without any success.
Cheers.
First, you might want to look into widgets and layers. You could define a layer for authenticaed users, and one for anonymous users, and attach widgets to those layers to achieve what you want. That might be the best way for you to accomplish this. Look in the Orchard docs for examples on how to do this.
I have done a similar thing before using custom controller and a lot of custom logic. Because of my specific requirements widgets and layers would not work for this. All the content on the page needed to change depending on some inputs, and widgets and layers were not going to be well suited for this. What I did was create a custom controller, and a corresponding Route with a high priority (so the Route takes precedence over any others that want to be the home page). I didn't mess with IHomePageProvider at all.
In the controller action I pulled the data necessary, and created the shapes I wanted, and then returned a result like this: return new ShapeResult(this, homePageShape);
homePageShape is constructed like this, right before the return statement:
// Create personalized home page shape:
var homeShape = _orchardServices.New.CustomHome(
SomeShape1: someShape1
, SomeShape2: someShape2
, SomeModel1: someModel1
...
);
This creates a shape called CustomHome, and orchard will automatically look for a template called CustomHome.cshtml in the views folder of your module.
I created several shapes (all the "someShapeX" vars you see above). Mostly they are created from content parts via the BuildDisplay() method. The content parts are queried using IContentManager, and the shapes are created like this (this example is for a slide show shape):
dynamic sliderShape = _contentManager.BuildDisplay(sliderPart, "Detail");
You can put logic in the controller to build the shapes you want depending on whether or not the user is logged in. In CustomHome.cshtml you would render a shape like this:
#Display(Model.SomeShape1)

Resources