How to route an url to local directory? (Description inside) - c#

I had a site in which all the images are inside a sub directory in my site. But I need to save those pictures in a specific directory and I need to show the in a client application which holds my page url.
for example: I had an url, http://foo.example.com/images/happy.jpg
the site is in c:/Example.
i'll store the images in c:/Images/categories/category1/happy.jpg
if my client request the above url, I need to redirect it to the image in the folder, where I kept all my images.
can somebody say is this possible? If possible, please suggest a way to accomplish this through url routing

The easiest way to accomplish this is to add a virtual directory to your IIS configuration for the folder where the images are held. Once the virtual directory is added you can reference the files via a URL to the virtual path. This is the best way in a production enviroment.
Technet step by step guide to adding a virtual directory
As for no authorization, you should select "Application User" (pass-through authentication). If you nest the new virtual directory inside the application's VD, you may need to add a web.config file to the virtual directory to override any authorization/deny rules in the root web.config file.
For local debugging, if the images are on your local PC, you can use a file:/// url replacing c:\ with file:/// and forward slashes instead of back slashes for the rest of the path. Don't forget to escape the slashes in the code behind by using "#".
Alternatively, a more involved solution would be configuring local IIS similar to above and using local IIS instead of the ASP.net runtime server which uses localhost:port using the link above and configuring your project to use IIS. If you are building this project over a long period of time local IIS makes the most sense. Short projects can be easily done without local IIS.
How to configure a project to use Local IIS for debugging

Related

How to get the URL of a file uploaded on a server in C#.net web application?

I am presently working on a C#.net Web Application which supports file uploads to the server by users. I am using an external WhatsApp API which needs to access these files using a URL. So, I am wondering as to what URL should I pass to the API so that it can access files on the server.
Will the URL be something like: base_url/file_path/file_name?
Also, what changes will I have to make in the route configuration file for the same (because, presently the default url pattern is base_url/controller/action)
Based on the above discussion, i can see 'DocumentMessage' is the model where its asking. I have 2 ways to handle. you can pick any based on your system design.
This is the approach where your app don't need any Cloud involvement. Irrespective of Hosting in IIS(on premise) or any Any Cloud service app, it will work. But only requirement is File need to be there in the Project folder structure.
Solution:- Create a Virtual path under your root directory. So your api/ website will be hosted under some domain(https://example.com) . So your Virtual directory path will be something like (https://example.com/fileLocation). So your entire file path will be published under this link. so for your File the path will be https://abc.or/fileLocation/file_Data
Note:- Disable the Directory access for your security purpose. Without fail
Reference:- For IIS Virtual Path
For Azure App service
If possible put your file in some Cloud storage(e.g. Blob storage container) & fetch the blob Url. you need to store the URL in the Database(for reusing it or storing instead of calling multiple time to Cloud to get the path).

Issue in routing in existing ASP.NET application

I have a problem with routing in an existing ASP.NET application for work. I have the code for the application and i can build and run it. When i run it I see the home page, but all links and images have invalid URL's. For each URL the same problem occurs.
For example an image on the home page has this src:
src="/appName/Images/img.jpg".
but the image is actually at "/Images/img.jpg". Notice the missing '/appName', this is the problem with every link / image.
The root folder of the application is in the folder '/appName', but the URL's/images seem to think it is one folder above that.
Now I could change every URL and take out the '/appName', but I feel like it should be possible to run the website correctly without changing the code. Maybe i should change something about the environment or IIS express.
HttpContext.Current.Server.MapPath("~") returns the path to the /appName folder. So the img src and a href start looking in /appName/appName which doesn't exist.
I have searched online, but cannot find anything.
I've already tried changing the root URL in applicationhost.config, but this resulted in IIS not finding an application in that folder.
I know the website works in the production environment, though i currently have no access to the production environment. But i do think it is an environmental setting (maybe in IIS).
So does anybody have an idea how to change it so the links think the root URL is one folder above the actual root folder in IIS?

ASP.Net - Unable to access Downloads folder from a web application

This relates to my previous post.
https://stackoverflow.com/questions/45937117/c-sharp-unbale-to-access-
downloads-folder?noredirect=1#comment78832001_45937117
What I want is to access "Downloads" folder from an ASP.Net web application.
string pathUser =
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string pathDownload = Path.Combine(pathUser, "Downloads");
string commentImagePath =pathDownload +"\\test.png";
Then I realized that the above code works in a Desktop application.
That is the UserProfile is available in that "Environment", but not in the web application's environment.
I have some download happening, and the content will be downloaded to the "Downloads" folder.
That is why I need the "Downloads" folder . I need to access that content.
Please help me with this.
Think that the Downloads folder is different for each user in the system , so you'll get c:\users\usera\downloads and c:\users\userb\downloads. So when you mean Downloads, you actually mean a different physical path.
At the same time, your ASP .NET application, if hosted in IIS will impersonate a specific user, so make sure that user has enough privileges to the path of interest. Also consider that the Downloads folder might be a folder that Windows will protect by default from being accessed across accounts.
I think you'd be better off saving files to a specific folder relative to your root of the website. Otherwise try to map specific folders from your drive as a Virtual Directory in IIS so the site would see it as relative to root, whilst in reality it lives somewhere else.

Access a website folder from another website in the same IIS

I have 4 independent ASP websites deployed in IIS server.
I access my website images using relativePath so i have root/path_to_files (something like that).
My problem is i want to access website1 images folder from website2 and so on, web3 access web4 (you get the picture).
I tried absolutePAth but security reasons (not safe) blocked me.
I`m reading the name and path of the repositories from a file.
What can I do, without remaking all or create a central repository(and upload all files there).
EDIT 1: using url is locked but i can unlock.
I can use url to get/list folders and images and use them ? Like resolve a url in a relativePATH to other website?!
Central repository the site is allready in production it will have big impact time/benfit to do that.
Have you considered symbolic links? You can make a shared image folder. Then each folder can see that "image" folder as its own.

Issue while opening local folder from ASP.NET web application

I have built an intranet web-application that is only used inside the company - so security issues do not matter . where we have a link in a web-application that should access a local directory on the local filesystem. When we pass parameter link a windows explorer instance should open with given a directory open.Is there anything i need to do with IIS?
System.Diagnostics.Process.Start("explorer.exe",#"c:\teste");
If you do that you'll be trying to spawn an explorer process on the server hosting the web application.
Do you just want to link to a path on the users local machine or at a network location?
Link to a file share through an anchor tag

Categories