There something I still do not understand about how the Content folder works in ASP.NET MVC. To make things clearer I have a few questions:
Is the Content folder the root folder? I mean does http://localhost/ point to Content or is it something else?
I have a file named dummyIcon.png inside Content/images/temp folder. How do I locate it from my domain layer (which is a Code Library project)?
What is the best practice of displaying images in ASP.NET MVC? Should I store a path to the image in the database (which I personally prefer), or do I save a byte array and return it to the view?
I found the following links to be helpful within the context of the MVC web application, but I'd still appreciate some answers to the questions posted above. Thank you.
Can an ASP.NET MVC controller return an Image?
how to display image using view and controller by ASP.NET MVC
Anything in the root will point to the root if it is ignored by your routes:
If you have an image placed on the on the root of your project. Then, say http:://localhost/dummy.ico" will give you a 404, no controller found. Until you do this in your global.asax.cs:
routes.IgnoreRoute("dummy.ico");
//you could add wildcards here to match different things
From Code if you use says File.Open(); you need the physical path to the file. You get it like this:
string filePath = Server.MapPath(Url.Content("~/Content/Images/Image.jpg"));
It is upto you here, although I would say, putting files into the database makes a lot of sense if you want everything in one place. If you need to move your app around you would just move the data base.
When it comes to file paths, please remember you don't want duplicate file names, so you will have to give each file a GUID and then link it up. It could make sense if you have a large number of files (or large files itself) so you're database won't grow like crazy.
HTH
1.Is the Content folder the root folder?
I mean does http://localhost/ point to
Content or is it something else?
No, http://localhost:port/ does not point to content folder. You can access files in content folder through http://localhost:port/content/...
2.I have a file named dummyIcon.png
inside Content/images/temp folder. How
do I locate it from my domain layer
(which is a Code Library project)?
You should be able to access it as http://localhost:port/Content/images/temp/dummyIcon.png
3.What is the best practice of
displaying images in ASP.NET MVC?
Should I store a path to the image in
the database (which I personally
prefer), or do I save a byte array and
return it to the view?
Where you store the images depends on your application needs. Are these generic images that is used to display application images (icons, company logo, etc).. Then it is best to store them in file system.
If your application deals with images and you work on storing images, manipulation etc, then you may need DB. I think, storing images used on the web application is a overhead.
You should make a model object for your controller to return. in this example i am returning SearchPageModel, a class i have created. but lets say this object has a property called imageURL
but make sure the controller actually returns an ActionResult
so for example...
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Search()
{
SearchPageModel Model = new SearchPageModel();
// populate the Model properties
Model.ImageURL = "myjpeg"
return View("Search", Model);
}
i then pass this model object back to my desired view in this case my "Search" View
and to display the image, in the view i would add..
<img src="Images/<%=Model.ImageURL %>.jpg" />
Related
I have 2 projects that are needing to talk to one another. The first is a ASP.NET MVC project, that when in production, has a feature where the user can edit an html template that is stored in the wwwroot folder of the project.
The second project is a C# console app that grabs some user data from a database, and then uses that data to email surveys to users. The html template from the first project is needing to be grabbed by this console app so that it can be used in sending out these emails. I was hoping to use HtmlAgilityPack to grab the html email template from the first project when it is live, something along the lines of this:
var web = new HtmlWeb();
var document = web.Load("www.sitename.com/EmailTemplate");
string text = document.ParsedText;
But I'm open to other ideas that might work in this case. More or less I think I just need to figure out how to access static html files from within the wwwroot folder from a browser path, if that's possible. Oh and these two projects are going to be running on different servers, so local paths won't work. Thank you!
In large part thanks to ADyson's comments, the course of action that makes most sense in this situation is to create a small API within the MVC app, that fetches the html file, and the console app will call this API to retrieve the needed html.
I had a similar problem, and I did it by adding ~/ in the beginning of my static file addresses in my _Layout.cshtml.
My template files and photos were no longer loaded in the project but the layout was loaded. This way the files were also loaded
I am using ASP.NET MVC5 and I would like to render a view from a folder that is outside the application folder. I tried registering my own custom the VirtualPathProvider and I even created my own VirtualPathProviderViewEngine to support rendering pure html pages. I have the latter working but cannot get the former to work. When I navigate to the route in question, I want MVC to check the internal Views folder for the View and then if it is not found I want it to look in the external folder.
When I step through the code, FileExists gets called for files that are in the Views folder and then the ViewEngine code runs but for a View that lives externally, the FileExists check runs and then I get a 404 on the screen. It does not ever get into the ViewEngine code. I know I am missing something simple here.
I am attaching a screenshot of what the sample folder structure would be. Any help would be greatly appreciated.
You can override the VirtualPathProvider and the VirtualFile
Check this link for an Example
I currently store a number of document preview images (jpg/tif) outside of my web root. There are 100s of them, so having this work efficiently is important.
The reason they are stored outside of the web root is that they contain data the only specific users/user groups may view (but each user can have 100s of documents they can view).
My current implementation is, when the user selects ‘view image’ an ajax call is triggered and this moves the image in question to a specific folder within the web root. The location is passed back and used to display the image to the user.
When the next image is clicked, the call deletes any existing images and copies over the requested image. At session logout / timeout the users image folder is emptied.
This has a few problems, but mainly:
Files are constantly being copied and deleted
There is the risk of images being left in the folder (issues with log off scripts)
The whole time an images is in the folder it could be viewed by another users (unlikely but possible)
Is there a better way of doing this? I looked at trying to combine the BinaryReader with the ajax call (as I hoped this would cut out the need to copy the files), but can’t see how to get the data back to be used by the JS in the calling page.
Alternatively is there a way of making selected Folders only accessible to given users based on some session criteria? (I can’t imagine there is but I thought it’s worth asking.)
So if anyone has any ideas on how this can be improved that would be great.
This is a c# ASP.NET app using Jquery.
Edit:
The image is displayed using ajax, this allows for preloading and also means the rest of the page does not need to be reloaded when they select the next/previous image.
It can almost be thought of as a javascript image swapper type situation, where the images are stored outside of the web root.
Thanks.
My current implementation is, when the user selects ‘view image’ an ajax call is triggered and this moves the image in question to a specific folder within the web root.
This is horrible idea. You realize you can just access the image data and pass it to web as stream with specific mime type, right?
Maybe try to write a method that will check user credentials by cookies, if it is not OK then load and send back some standard image that will say that user must log in to view file, if it is ok then load and show proper file from a path outside of root based on url parameter (with proper headers like content-type also often referred as mime-type ofc). Then link urls to that method with proper parameter(s).
You can easily find examples of code (like here) to display image in binary form from DB. You would need just to load them from some path outside of root, not DB.
Also you don't need to load it by AJAX - just add IMG with SRC pointing to URL of handler. Or redirect / open window if it needs to be downloaded not shown.
The issue was how to get an image to show via javascript that is not in the web root.
I created a generic handler (ashx file) that based on the session values (authentication) and submitted parameters would return an image.
That in turn is being called via AJAX.
I need the same thing as this guy here:
http://forums.asp.net/t/1507682.aspx
The thing is that the answer he marked as correct doesn't work for me. I am using ASP.NET MVC3 soon to be migrated to MVC4 and Windows Server 2012.
I want to prevent direct access to some images because of security issues. It's not Image Leeching protection I'm worried at this moment, I read several articles on how to do that.
If the link is on my page like this
<img src="/Content/Images/img1.jpg" />
The image should be shown, but not if it is directly accessed:
http://mywebsite.com/Content/Images/img1.jpg
Since we're talking about images and several people accessing it, a solution that considers performance issue would be my way to go.
Any ideas on how to implement that?
Thanks.
You probably want to think outside MVC and handle hotlinking in IIS itself. IIS has the tools (the referer (sic) header) to figure out whether or not an image request appears to have come from inside your site or not.
Write an Action method which reads the image from disk / db and render it. In that action method, you may check whether the user is authorized or not and return the appropriate image or a "not authorized" image.
public class ProductController : Controller
{
public ActionResult Photo(string id)
{
// TO DO: get image using the unique id passed and render it
}
}
You can use the path to this action method as your image path.
<img src="#Url.Action("Photo","Product")/someUniqueIDOfPhotoFromYourDB" />
This approach will have a slight performance impact compared to serving image directly from disk.
I've been learning MVC3 a bit for a while and although I understand most of the basics, the routing is something I just can't seem to get a hang off. I'm not sure whether I'm doing/thinking it in a correct way and the other answers I've read on stackoverflow/google only seem to confuse me more.
Anyway here's the situation. I've got a solution (cleaned it up for this question), where I'd like to create a structure in my views folder to organize everything in a structure I've been using for my asp.net webforms projects.
Anyway, I was wondering whether it would be possible to get this kind of structure, where I could organize all my partial views into a specific subfolder called UserControls in which there are more subfolders where I would group every partial view I'd need for a specific page.
Ideally, my Views folder would contain 2 folders: Pages and UserControls and everything I'd need would go into a subfolder, or a sub sub folder.
Okay so if this is possible, how would I start routing this? I've been trying multiple ways of getting any result but they all end up in a 404 errors.
If this is a wrong approach of me, what would be a better alternative?
Thanks in advance!
Logically, this is better served by grouping your partials in with the actual views like so:
/Other
/Partials
OtherPartial1.cshtml
OtherPartial2.cshtml
Index.cshtml
/Some
/Partials
SomePartial.cshtml
Index.cshtml
SomeOtherView.cshtml
Now, you can certainly do as you suggest, but it simply means that your helpers in other views have more text to write to get where they want to go:
IE.
#RenderPartial("Partials/OtherPartial.cshtml")
VS
#RenderPartial("~/Views/PartialControls/Other/OtherPartial.cshtml")
You could manually return the view file for each actionresult or partialactionresult.
I'd also recommend changing "UserControls (PartialViews)" folder to just PartialViews.
e.g.
public PartialViewResult OtherPartialView1()
{
return PartialView("../PartialViews/OtherViews/OtherPartialView.cshtml");
}