问题
In VS 2015, when you create a new MVC 6.0 application using this approach:
File-->New-->Project-->ASP.NET Web Application-->ASP.NET 5 Preview Templates
You end up have the following file structure on disk:
- artifacts
- src
- MyProject.sln
- global.json
Instead if I decide to create a blank solution first like so:
File-->New-->Project-->Other Project Types-->Visual Studio Solutions-->Blank Solution
And start adding a new ASP.NET Web Application project to this solution; you end up having a file structure that doesn’t have a global.json
file and no src
folder.
According to the documentation, the global.json file is used to configure the solution as a whole. It includes just two sections, projects and sdk by default.
The projects
property designates which folders contain source code for the solution. By default the project structure places source files in a src folder, allowing build artifacts to be placed in a sibling folder, making it easier to exclude such things from source control.
The sdk
property specifies the version of the DNX (.Net Execution Environment) that Visual Studio will use when opening the solution. It’s set here, rather than in project.json, to avoid scenarios where different projects within a solution are targeting different versions of the SDK.
Question 1)
As mentioned above, if I choose to first create a Blank Solution, I won’t be having a global.json
file.
Is not having a global.json
file impact the application’s behavior in any shape or form? For example when I deploy or the interaction with the build artifacts?
Question 2)
If it does have an impact, then should I manually be creating this global.json
file?
Question 3)
As of this writing, is there a recommended approach for creating a multi-layered application in ASP.NET 5?
Should I first create the MVC project and start adding Class Libraries to the project?
Or
Should I first start by creating a Blank Solution and start adding the Class Libraries and the Web Application to the solution (knowing I won’t have global.json
file nor a src
folder)?
回答1:
As you've noted, this seems to be the new way of creating solutions when targeting ASP.NET 5. The global.json file specifies some solution-level settings. In particular, it defines the DNX runtime settings.
I've seen two approaches to the src / test folder structure. If you look at what the MVC team is doing, they incorporate the src / test folders into the repository root. Many projects on, e.g., Github have a src directory in the root. So as long as you're OK with your global.json and solution files in the root of your project, it seems to align well with existing practices.
If you look at what the .NET team is doing with the .NET 5 Core Framework, you'll see a similar pattern, albeit without the global.json file. Many of their libraries have src and tests subdirectories.
I am going to try to pattern my projects after what the MVC team is doing for the time being, until I have a better understanding of .NET 5, DNX, and the rest. It does impose a certain structure on your projects, but then so did VS 2013 and .NET 4.5.
回答2:
The src
, test
, etc. folders allow you to group projects by type. This helps keep the solution maintainable when there are a large number of projects. Here is an example of various project types that have been proposed.
Per the ASP.NET Core Engineering Guidelines: "By default project-to-project references must be sibling folders. Using a global.json
file allows a solution to specify non-standard locations to locate references."
To sum it up, if have lots of projects, you can group them by type into folders and use global.json
to allow projects in one group to reference those in another.
回答3:
1) Yes, for me I also had this issue and started with blank solution. Then my projects had issues referencing each others.
2) For me it did not work without global.json. You also need to make sure the solution file points correctly to everything, like the global.json. The projects did not build and did not find each others for me otherwise.
3) To get everything working I created a mvc core project first to get the global.json file. Then I renamed the scr folder to applicationname.web. Make sure to alter the solution file accordingly so it does not point wrong. Also you should update the project.json file. This way, all thou I wanted a blank solution like you, I created a default project and then altered it to how I wanted it, that worked for me.
I will build my current app using Onion Architeture as a guideline. I see no issue doing this with core. This also means that I will build the IoC in a different project (probably named infrastructure.IoC) so that my web project does not contain references to projects containing implementations of interfaces that the web should not know about.
Also, if you still have problems, I actually got local build and reference problems because I had resharper and I turned that off, then the reference problems between projects went away and I could build successfully.
来源:https://stackoverflow.com/questions/31566000/how-important-is-the-global-json-and-src-folder