简体   繁体   中英

Share a common image folder between two mvc projects

I have two MVC applications - Admin and User. Admins would be uploading the images which then would be displayed in the Users application. The images path would be stored in the db, which is shared between the two apps, and using the path the Users app will fetch the image. How would I make the image folder accessible to both apps in Visual Studio and also once the site is uploaded on to the server?

That's really two separate questions:

  1. How do I share the folder in production?

    If the sites are on the same server, you can put the shared images pretty much anywhere on the same server. If they are on different servers, you would want to map a network location on both servers to wherever the images are located. In either case, you would then create a virtual directory in IIS on both sites pointing to that shared location.

  2. How do I share the folder in development (ie Visual Studio)?

    That's a bit more tricky. Basically, you would need to handle it in the same way as in production, but the problem is that IIS Express is not nearly as transparent as full IIS. I've employed the following method in Visual Studio 2012 and 2013, but the last time I tried it in 2015, it no longer worked. That may have been fixed by now, though, as it's been a while. Regardless, this is pretty much your only option.

    1. Right-click on your solution in the Solution Explorer pane and choose Add > New Website... .
    2. Click the Browse... button on the resulting window.
    3. Click the Local IIS tab.
    4. Find the first site in the list that you want to utilize the shared image directory on, and click on it to highlight it.
    5. Then, in the top right of the window, click the icon that looks like a folder with a star on it. If you hover over it for a second, it will say "Create New Virtual Directory".
    6. In "Alias name:", enter what the path portion within your application should be. For example, if you wanted to find the images at http://localhost:12345/SharedImages , then you would enter SharedImages . Make sure that whatever you choose is not an actual existing directory within your project, though.
    7. For "Folder:", browse to the physical directory where the shared images are at on your computer.
    8. Uncheck the Create IIS application checkbox.
    9. Click OK .
    10. Repeat steps 4-9 for any additional sites.
    11. Click Cancel on the previous window.
    12. Click Cancel again on the original Add New Website Window . (You're not actually going to add a new website to your solution. This is just the only way to access the place where you can create this virtual directory.)

    At that point, you should be back to the main Visual Studio window. You won't actually notice any difference in your project/solution, as this was all done in IIS Express. However, if you browse your website and try to load an image under the virtual directory, you should be able to see it in your browser, ie:

     http://localhost:12345/SharedImages/foo.jpg

    If you see the image, everything is working fine.

If you provide the full url to the users app it will fetch the images no need for other configurations. But be sure that both apps are running and that you are providing full URL not file PATH

[UPDATE] This will work:

Http://domain.com/folder/file.ext

This will not work:

D:\\Hosting\domain\folder\file.ext

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM