Answered by:
Best method for setting image path in MVC

Question
-
User-1684154527 posted
Hi,
Can i set image path inside the BuildleConfig File.If it is not possible,then provide any other method to set image path in a better way.
Tuesday, August 20, 2019 10:39 AM
Answers
-
User753101303 posted
Seems quite different from how to best "set an image path".
"Securing path" is still a bit vague. If you mean that you want to have files not being available to anonymous users your best bet is likely to store them outside of your web site and to serve them using a controller (returning a class derived from https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.fileresult?view=aspnet-mvc-5.2). Your file content could even come from a db if you want.
You'll have then exactly the same options than for any controller (new to that as well ? most often you can require authenticated users everywhere using a "global filter" configuration and you then allow anonymous users only where needed).
Or do you even have an app that uses authentication to start with ?
BTW if you are using ASP.NET Core it could be best to post your next questions to the dedicated https://forums.asp.net/1255.aspx/1?ASP+NET+Core forum.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 20, 2019 6:27 PM -
User1520731567 posted
Hi Muraleedharan,
As @PatriceSc said,you could set up permission management.
If you have permission, you can see the picture. If you don't have permission, you can't see the picture.
I think this is another measure to protect the picture.
You could set the judgment in Application_AuthenticateRequest in Global.asax.cs
You could determine the condition and decide if you want to display the picture,for example:
protected void Application_AuthenticateRequest(object sender, EventArgs e) { if (xxxx)//Determine if the current user has permission { ..do sth... }
else
{
HttpContext.Current.Response.WriteFile(HttpContext.Current.Server.MapPath("~/xxx/forbid.png")); //if not,show forbid picture
HttpContext.Current.Response.End();
} }Best Regards.
Yuki Tao
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 21, 2019 6:57 AM
All replies
-
User753101303 posted
Hi,
Better way than what ?
AFAIK the usual approach is to use src="~/folder/file.png". The tilde is the "application root" and so can work even if your app is installed under a web site root (ie at https://site.com/myapp rather than just at https://site.com). This is what you are using and you have some concern with that ?
Or do you mean that your image files are stored on a network share or whatever ? In this case the network share could be configured as a virtual folder in IIS or you could have a controller fetching images found under whatever image root (configured as part of application settings maybe) you want so that images are always found at the same ~/image/file.png path regardless of the file actual physical location.
Or it seems you are using ASP.NET Core (you have also a dedicated forum for this) and you try to have an ImageRoot in addition to the ContentRoot and WebRoot ?
Always try to include enough information in your first post (for a general programming question, a short description of the current situation and which problem you are trying to solve) so that you can increase the probability that someone could answer without having to ask more details first.
Tuesday, August 20, 2019 11:42 AM -
User-1684154527 posted
Hi,
Better way than what ?
AFAIK the usual approach is to use src="~/folder/file.png". The tilde is the "application root" and so can work even if your app is installed under a web site root (ie at https://site.com/myapp rather than just at https://site.com). This is what you are using and you have some concern with that ?
Or do you mean that your image files are stored on a network share or whatever ? In this case the network share could be configured as a virtual folder in IIS or you could have a controller fetching images found under whatever image root (configured as part of application settings maybe) you want so that images are always found at the same ~/image/file.png path regardless of the file actual physical location.
Or it seems you are using ASP.NET Core (you have also a dedicated forum for this) and you try to have an ImageRoot in addition to the ContentRoot and WebRoot ?
Always try to include enough information in your first post (for a general programming question, a short description of the current situation and which problem you are trying to solve) so that you can increase the probability that someone could answer without having to ask more details first.
Hello
I want to secure image path in asp.net mvc.protecting the image urls.
Tuesday, August 20, 2019 12:25 PM -
User475983607 posted
I want to secure image path in asp.net mvc.protecting the image urls.ASP.NET Core's openly published static file handler documentation covers this topic.
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2
Tuesday, August 20, 2019 1:19 PM -
User-474980206 posted
you can not secure the path. the browser tools will give easy access.
Tuesday, August 20, 2019 1:57 PM -
User753101303 posted
Seems quite different from how to best "set an image path".
"Securing path" is still a bit vague. If you mean that you want to have files not being available to anonymous users your best bet is likely to store them outside of your web site and to serve them using a controller (returning a class derived from https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.fileresult?view=aspnet-mvc-5.2). Your file content could even come from a db if you want.
You'll have then exactly the same options than for any controller (new to that as well ? most often you can require authenticated users everywhere using a "global filter" configuration and you then allow anonymous users only where needed).
Or do you even have an app that uses authentication to start with ?
BTW if you are using ASP.NET Core it could be best to post your next questions to the dedicated https://forums.asp.net/1255.aspx/1?ASP+NET+Core forum.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 20, 2019 6:27 PM -
User1520731567 posted
Hi Muraleedharan,
As @PatriceSc said,you could set up permission management.
If you have permission, you can see the picture. If you don't have permission, you can't see the picture.
I think this is another measure to protect the picture.
You could set the judgment in Application_AuthenticateRequest in Global.asax.cs
You could determine the condition and decide if you want to display the picture,for example:
protected void Application_AuthenticateRequest(object sender, EventArgs e) { if (xxxx)//Determine if the current user has permission { ..do sth... }
else
{
HttpContext.Current.Response.WriteFile(HttpContext.Current.Server.MapPath("~/xxx/forbid.png")); //if not,show forbid picture
HttpContext.Current.Response.End();
} }Best Regards.
Yuki Tao
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 21, 2019 6:57 AM