Path To Files Inside Content Folder (ASP.NET MVC)

前端 未结 3 824
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-04 01:19

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:

  1. Is the Content folder t
3条回答
  •  野性不改
    2021-02-04 01:45

    1. 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
      
    2. 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"));
      
    3. 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

提交回复
热议问题