"ASP. NET Core" on directory browsing

Source: Internet
Author: User

What is "elementary"? is a word not finished, at most two words will be introduced, and then directly to the interpretation of the example of the way. To simplify, from 7,000 years ago to now, from the ancestors to us, has been pursuing the ideal goal, as far as possible to make complex things simple.

The old week tells you a way to show your programming is very powerful-that is, programming with people who do not understand programming, if you can not understand the person to understand, then your strength is up. is full of fun and can make you full of momentum of the practice, is that you and sister programming, if you have the ability to use programming to amuse her, old week to you.

OK, the above content is purely extraterrestrial theory, the following will start to talk about directory browsing things. Plainly, that's it: you enter a URL with your browser, and the server returns a list of directories and files in the form of an HTML page. As shown in this HD uncensored no watermark.

This Web access way you must be very familiar with, before, in IIS, especially the past ASP, if the default page can not be found, often let you browse the directory.

This is what we are going to say today in directory browsing, of course, that is how to implement it in ASP.

The old Zhou said, the complexity of the simple, not to mention the Core is very concise, so, you learn the content of this article without any theory to know, you will only copy the code on the line, below I directly tell you how to get.

1, please prepare four or five picture files, this is used for testing. Of course, other documents can also, the reason I choose the picture file, because the file is not big, but also can be eye-pleasing.

2, the new project (blank Can).

3, after the project is established, the project template will default to you to generate a directory called Wwwroot. The default template has this, and if not, you use the VS is pirated.

4, create a new directory under the Wwwroot directory, called images.

5, in the new images directory, the front of the picture to put in.

After you have finished, you think your directory structure is probably the case, do not need to do the example of the dead, you can learn something good.

All right, tell me what kind of toy this Wwwroot directory is. A word: It is used to place static Web resources.

Hammer the blackboard, draw the key, the final exam does not test. Notice the "static", what is static? is not moving Bai, what is not moving, such as JS script, image, video, audio, style sheet ... Plainly, in a nutshell: Do not need to be executed on the server.

The corresponding, of course, is dynamic resources, what is dynamic? Of course, it will be done on the server, such as Razor page/view, code files and so on.

If you can understand the above, you will understand, that is to say, the person who is wandering in your shop can put in the Wwwroot directory, but you can't put your wife there.

Open the Startup.cs file and turn on the Directory browsing feature on the Startup.configureservices method.

         Public void configureservices (iservicecollection services)        {            services. Adddirectorybrowser ();        }

This you have to remember, a lot of friends will forget. This is the function (service) registration phase, registered to use.

Then, use directory browsing in the Startup.configure method.

         Public void Configure (Iapplicationbuilder app, ihostingenvironment env)        {            app. Usedirectorybrowser ();        }

After the above two steps, completed, yes, it is so simple. Run the chant. After running, in the browser address bar directly enter the root URL, you can see the Wwwroot directory in the east.

You can click on the images subdirectory to go in and play.

Did you see the papers that you just prepared? If you see it, your code is fine, if you don't see it, check your character.

At this time, you will ask, hey! I don't have a wwwroot in the path? Yes, because this is really like a previous virtual directory, its path description is not necessarily the same as the physical path. Also, by default, the browse directory is rooted in the Wwwroot directory, so you can see the images subdirectory as soon as you enter the root URL.

Next, you are curious about the location of the file above, want to open or download. However, you are disappointed ... Familiar and unfamiliar 404.

Whether programming or chasing girls, you must remember: do not shout when encountering problems, never dozens. Even mother is not a thing to scold, besides, shouting loudly is the performance of the ill-bred. Below, the old week leisurely to tell you the reason for the emergence of Dear 404.

Originally, Usedirectorybrowser Just let you access the directory only, but did not let you access the file Oh, alas, blame God, blame old weeks do not explain in advance. The old week is deliberately not explained, if said in advance, you will not learn this skill.

OK, start to solve the problem, very simple, the Static File function can also be used. The front that lets you look at the directory, now this lets you see the file, put two together, that is the catalog file to see together. Of course, by the way, if you want to combine these functions together, there is a better routine--usefileserver, for example.

    App. Usefileserver ();

File Server combines directory browsing (which is closed by default) with static file access, as described later in this article.

Now let's talk about how to solve the problem just can't access the file, yes, use the Static file on it.

         Public void Configure (Iapplicationbuilder app, ihostingenvironment env)        {          app. Usestaticfiles (); App. Usedirectorybrowser ();        }

OK, now try again.

But, you may think, I do not seem to have to expose the whole wwwroot to others, must leave some privacy, or my static resources may not be in the Wwwroot directory.

In this case, you can configure the options.

         Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment Env) {app. Usestaticfiles (Newstaticfileoptions {Fileprovider=NewPhysicalfileprovider (Path.Combine (Directory.GetCurrentDirectory (),"wwwroot/images")), Requestpath="/files"            }); App. Usedirectorybrowser (Newdirectorybrowseroptions {Fileprovider=NewPhysicalfileprovider (Path.Combine (Directory.GetCurrentDirectory (),"wwwroot/images")), Requestpath="/files"            }); }

For the Fileprovider attribute, we generally use the Physicalfileprovider class, noting that absolute paths must be used. Because different system directory structure is different, such as Linux is different from Windows, so get the current directory path, it is best to use the Directory.GetCurrentDirectory method.

The Requestpath property is similar to specifying a virtual directory, you can set a relative URL for an absolute path, and then you can access the corresponding directory through this relative URL. For example, the relative URL is/files.

In order to access the file while browsing the directory, the Usedirectorybrowser is identical to the configuration in Usestaticfiles, for example, the physical path specified is the same, and the relative URL assigned is the same.

Now, run the project and you will enter http://localhost:5000/files/to access the directory.

At this time, you can not see the name of the images directory, it becomes files. This hides the real directory to some extent.

If you feel this kind of trouble, as mentioned above, you can close up and use File Server directly.

            App. Usefileserver (new  fileserveroptions            {                new"wwwroot/images " ) ),                "/files",                 true             });

Note that the Enabledirectorybrowsing attribute inside must be set to true, otherwise it will not be able to browse the directory.

Well, today's topic is going to be here. Recently wrote a blog less, no way, things more.

"ASP. NET Core" on directory browsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.