In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what is the role of the two configuration files in ASP.NET MVC, I hope you will gain something after reading this article, let's discuss it together!
Preface
After creating a new MVC project, you will find that there are two web.config files in the entire project structure, as shown in the following figure:
What is the difference between these two configuration files, one under the root of the project and the other under the Views folder?
I. the configuration file under the root directory
The web.config configuration file code under the directory is as follows:
This configuration file is mainly used to configure database connection strings, log output paths and other information, such as configuring database connection strings.
Configuration files under the Views folder
The configuration file under the Views folder is mainly used to introduce some namespaces in cshtml pages.
In the previous article, if we were to use the Student entity class in the cshtml view page, we needed to first introduce the Student namespace into the page:
If all cshtml pages need to use the Student class, then each page needs to introduce the namespace of the Student class before it can be used, so there will be a lot of repetitive work. You can add the namespace of the Student class to the configuration file under the Views folder, so that you do not need to introduce the namespace of the Student class on every page.
Then modify the Index view corresponding to ViewDataDemo as follows:
@ * introduce Student namespace * @ @ * @ using MVCStudyDemo.Models; remove the introduction of Student namespace and introduce * @ {ViewBag.Title = "Index" in the web.config file / / the Razor syntax is used here, and the background C# code / / ViewData's value is of type Object, which requires type conversion / / the conventional way of writing is to first type convert var list = ViewData ["Data"] as List;} to View through ViewData 1, passing the string other:@ViewData ["Other"] 2. Pass the string name:@ViewData ["name"]; 3. Pass the string age:@ViewData ["age"] 4 、 Pass the collection mode one @ foreach (var item in list) {ID:@item.ID Name:@item.Name Age:@item.Age Sex:@item.Sex Email:@item.Email} 5, Transfer collection mode two @ foreach (var item in ViewData ["Data"] as List) {ID:@item.ID Name:@item.Name Age:@item.Age Sex:@item.Sex Email:@item.Email}
Note: after the namespace is removed from the Index view, the Student entity class will be marked red and will not affect the program.
Rebuild the program, and then run:
After reading this article, I believe you have a certain understanding of "what is the role of the two profiles in ASP.NET MVC". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.