ASP.NET appsettings.json does not exist in my project - c#

I am following this guide on how to create a Web API with ASP.NET Core and MongoDB. I have followed every step successfully up until the Add a configuration model section. In the guide, I need to locate appsettings.json and add database configuration values to my project. The problem is that the file they mention appsettings.json does not exist in my project.
In the guide, its implied that the file should already exist and should even already have content in it, so I don't know if I should just create it.
I have looked online and all over my project but nothing seems to be definitive on what is the right thing to do (some guides contradict each other, etc.).
Apart from the default ones, I explicitly installed have the following NuGet packages:
Microsoft.Extensions.Configuration (I thought this might help, but it didn't)
MongoDB.Driver (the instructions on the guide said to)
My setup is the following:
Microsoft Visual Studio Community 2019 Version 16.4.6
Microsoft .NET Framework Version 4.8.03752
ASP.NET and Web Tools 2019 16.4.462.24200
ASP.NET Web Frameworks and Tools 2019 16.4.462.24200
I have closed and re-opened VS but that didn't change anything.
This is my solution explorer:
Should I just create the file? In the guide, it already has content in it. What about that?
Is there a new/different way of add database configuration values to my project?
Is the guide outdated? If it is completely unusable, can you point me to a newer guide on how to Create a web API with ASP.NET Core and MongoDB?
Thank you very much!

The app exists for ASP.NET core projects. You mistakenly created, I believe, an ASP.NET framewok project. The tutorial is about the ASP.NET core.

Related

New to Visual Studio, program won't build

I am relatively new to Visual Studio, C#, and using Github. I am trying to get the example for AncestralTrees working (Found here: https://github.com/fire-eggs/Ancestral-Tree). I cloned the repo and try to build, but I get a set of errors that do not make sense.
First is that AppSettings.cs and MruStripMenu.cs could not be found. Are these default config files I have to provide, or am I missing something?
And second is that .NET framework v4.5 wasn't found. For this I am a bit confused, because I thought it was backwards compatible, and I have v6.0.301 installed. To try and fix this error, I went to the website provided and downloaded it, but it wouldn't install as I already have a newer version. Again, am I missing something?
Any help would be greatly appreciated!
For the compatibility issue, .Net Core (6.0) and .Net Framework (4.5) are different animals, with .Net Core as more of a successor to Framework. They are very similar, but there are some breaking changes between Framework and Core, and if a project was intended for .Net Framework 4.5 you will want to have it installed on your system and set as the build target for the project in Visual Studio. You could probably also build for 4.7 or 4.8.
For the files, if they are part of the repo I would expect them to be there. I'd check your file system and see if the files are there, and then check the repo on github and see if they are included in the repo. If not, you may need an additional repo or you may need to manually add a NuGet package.
I did notice this at the bottom of the github page:
This program is both a demo of my GEDCOM library: YAGP, and a hopefully useful program in itself.
Where the "YAGP" text is a dead link. So I would expect to also need this project, which either no longer exists or is not public. This is probably the correct address:
https://github.com/fire-eggs/YAGP
(I submitted a change with the fixed link.)

How can you create MVC and Web API project in the same project in JetBrains Rider?

I am want to create a .NET Framework project that has API and MVC parts to it. In Visual Studio there are checkboxes that I can select but in Rider there is nothing, it only allows me to create an MVC project.
I know I can just reinstall Visual Studio and do it from there but I really want to know if there is a way to do this.
With .Net Core I can simply add an API controlller bt I don't know how to just add a controller.
If there is a way to add it manually, please tell me!
Rider screenshot:
As you can see, there is no place to add API. I can't even create a standalone API project!
Status
Someone suggested that I create a WebAPI separately and add the config later but I can't even create an API.
I looked up how to add an API and it tells you to add one line of config but the code throws errors. The question
You can just create it from the type combo:

Can't find global application class nor Global. asax file

I want to add an API service to my existing .NET web application.
I was following this tutorial, but couldn't found any option to add web service.
Then I followed these two questions on Stackoverflow (Where is the Global.asax.cs file? and Global.asax file is missing on my Asp.Net Empty Web application project) but couldn't find the Add option for the global application class. How can I resolve this issue? Am I doing something wrong?
I tried to create a new solution, the problem is the same.
I'm using VisualStudio 2019 16.4.6
My project's targeted framework is .NET Core 3.1
If there is any tutorial or written document for adding web API to an existing project please give me, that would be very helpful. I tried to follow this blog and installed Microsoft.AspNet.WebApi.Core and got this warning -
Package 'Microsoft.AspNet.WebApi.Core 5.2.7' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETCoreApp,Version=v3.1'. This package may not be fully compatible with your project.
With ASP.NET Core, the entry point to an application is Startup, and you no longer have a dependency on Global.asax.
You can find more about it on this link
To consume an API you can use RestSharp in your Asp.net Core Application.
You can check this link to learn more about consuming an API using RestSharp in your Asp.net Core Application.
My project's targeted framework is .NET Core 3.1
Global.asax.cs will not be generated if you use ASP.NET Core. You have to use older version of ASP.NET, for example ASP.NET 4 (4.7.2) if you want to follow that old tutorial.
Or you can try a tutorial for WebApi using Asp.Net Core like this one: https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-3.1&tabs=visual-studio

cannot add controller in .net RestApi

I am trying to build a read only .net rest api using the following tutorial on Microsoft Docs-(https://learn.microsoft.com/en-us/aspnet/web-api/overview/older-versions/build-restful-apis-with-aspnet-web-api)
According to the tutorial when i add a controller the following error popup.
I have tried other solutions on stackoverflow like
Clearing ComponentModelCache
Also deleting .vs folder in solution
But nothing helped
Try changing your project to .NET Framework instead of .NET Core and make sure you are able to create a solution with .NET Framework. If the issue still persists please provide some additional log details of VisualStudio when creating with .NET Core

.net Core Solution - Project stucture

I want create a solution with VS Code.
All projects will be .Net Core projects. In this solution there will be shared library projects, a web API project, a react web site project.
Shared libraries will be used by other projects in the solution.
I couldn't find any good example to create solution similir to what I need.
Just download the community editon of visual studio 2019. It has a project template, that contains a .net core project providing the server side and a react app for the frontend. You can the easily add a library project. I would suggest you use .Net Standard for that.
New Project -> ASP.NET Core Web Application -> React.js
Edit:
Ok the official template looks like this:
SolutionFile
ASP.NET Core Web Application Project
ClientApp (Contains the react app)
Controllers (Your API controllers)
Programm.cs
Startup.cs
Library Projects
IMP: I do not have enough so reputation to add comment
keep the shared project outside of all other .net core app/library that has reference, and use dotnet add reference command to refer them where ever you need.
dotnet add reference
if you want to create solution file use, dotnet new sln command .
use dotnet sln command to change solution file created.
dotnet new
dontet sln
use workspace option available in VS Code. and save them(workspace) for feature reference and it comes handy during debugging.
hope this helps to solve your problem.

Categories