.net Core Solution - Project stucture - c#

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.

Related

ASP.NET appsettings.json does not exist in my project

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.

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

Entity Framework Tools not working with UWP apps C#

Startup project 'EFGetStartedUWP' is a Universal Windows Platform app. This version of the Entity Framework Core Package Manager Console Tools doesn't support this type of project. For more information on using the EF Core Tools with UWP projects, see https://go.microsoft.com/fwlink/?linkid=858496
I'm trying to connect SQLite Database to a basic UWP app for the sake of learning but when I try migration. It just keep giving me the above error. I've searched quite a lot on the internet but didn't get the appropriate answer. The Microsoft documentation is of no use in this scenario. Also I've installed Microsoft.EntityFrameworkCore.Sqlite & Microsoft.EntityFrameworkCore.Tools.
If anyone can suggest any other way to connect SQLite database to UWP that'll be very useful too as I'm quite new to C# and I've a university project to do on a UWP app.
I've just stepped on a similar issue, and I've been lucky enough to solve it. To start with, I found these links very useful:
Entity Framework Core tools reference - .NET Core CLI - Other target frameworks and Common options.
In my case, my solution ended up with a configuration like this:
the application project (Universal Windows) - startup project for the solution,
a class library project (.NET Standard 2.0) - with the models and context classes,
a dummy console app project (.NET 6.0) - necessary to act like a startup project for the tools.
Have into account that (at the time of writing) UWP doesn't support .NET Standard 2.1. It supports .NET Standard 2.0 from version 10.0.16299. This means the (Universal Windows) application project can't reference a .NET Standard 2.1 project (e.g. .NET 5.0, .NET Core 3.0, Mono 6.4, etc.), so the class library project CAN'T be .NET 2.1, .NET 5.0, etc. The opposite is not true: a .NET 5.0 or .NET 6.0 project can reference a .NET Standard 2.0 project, so the dummy console app project can be .NET 6.0.
Steps to add a migration:
In the class library project, install the Nuget package for the corresponding EFCore platform used (e.g. for SQLite, install Microsoft.EntityFrameworkCore.Sqlite). Do not install the most recent version but the last that depends on .NET Standard 2.0, which is 3.1.21 at the time of writing.
In the dummy console app project, install this Nuget package: Microsoft.EntityFrameworkCore.Design. For compatibility purposes, install the same version as the version chosen in the previous step (e.g. 3.1.21).
In the application project, add a reference to the class library project. (Right-click on the project, select Add > Project Reference..., and tick the class library project.)
In the dummy console app project, add a reference to the class library project. Note there's no need to change anything else in the dummy console app project, although the project doesn't have to be built (less deployed), so feel free to open the Configuration Manager, and untick Build (and Deploy). In addition, in the Configuration Manager, don't bother to change the platform (x64, Any CPU...) to be the same as the referenced class library project. After all, the dummy console app project won't be built, so ignore any warnings about the architecture not being the same as the class library project.
Rebuild your solution to make sure there are no other issues going on. If there are any, fix them before continuing.
Open the command prompt (tip: navigate to the root folder of your solution with Windows File Explorer and type cmd in the address bar to open the command prompt in that folder).
Type the following (-p signals the target project while -s indicates the startup project):
?> dotnet ef migrations add #MigrationName -s #DummyConsoleAppProject -p #ClassLibraryProject
Optionally, unload the dummy console app project, and load it again next time you need it to add a migration.
This looks to be a known issue from here: https://github.com/aspnet/EntityFrameworkCore/issues/9666
Using EFCore with UWP is a bit finicky.
So, add a new project to your solution -> select .net core console app -> create DBContext and your models there -> run the "Add-Migration" command and reference it back to your main project.
That should get rid of that error and let you use migrations with UWP.

ASP.Net Core - Where to find the Nuget packages installed list in the project

I have an ASP.Net Core 2.2 application.
MyApp.Sln
MyApp.API (ASP.Net Core 2.2 API Based project)
MyApp.Services (CL-Class Library)
MyApp.Contracts - (CL-Class Library)
My.Services.Tests - (CL-Class Library)
The above projects have different libraries(NuGet packages) installed
In .Net framework we used to have packages.config thats lists the nuget packages with the version details.
Where I can find the same details in .Net Core 2.2 ?
Because different project in one sln should not have different version of NuGet.
Thanks!
You can right Click in Your project in Solution explorer and Edit(For example MyApp.Services.csproj) in this file you will See Packages
You can follow this link to see options
Default command
Running in the project folder - Shows all packages, by default
dotnet list package <optional project path or solution path>
I don't believe there is anything equal to packages.config in .Net Core (possible reason - it aims to be more modular). You will have to make a little bit of work to solve your issue.
The quickest way to get dependecy graph is to run
dotnet msbuild YourProject.sln /t:GenerateRestoreGraphFile /p:RestoreGraphOutputPath=graph.dg
from terminal. Then you can open it with any editor and view all your dependencies in one file.
If this file isn't enough for you, you will (unfortunately) have to do a little bit of dirty work. See this answer View nuget package dependency hierarchy for some really good solutions, like how to write your own application to print dependencies, or how to use NPM.

How to Deploy/Publish two projects in ASP.NET MVC with VS (Codeplex: AspNet.Identity.MySQL)

I have created an ASP.NET MVC 5 project with Entity Framework and a MySQL database. In Debug-mode everything works perfectly, but now I would like to publish it to my IIS-Server.
The first project in my Solution is AspNet.Identity.MySQL which
allows me to use Entity Framework in combination with Identity 2.0
and MySQL. It is from codeplex. https://aspnet.codeplex.com/SourceControl/latest#Samples/Identity/AspNet.Identity.MySQL/
The second project is my ASP.NET MVC application.
Publishing the second project by itself is no problem. I just don´t know how to publish the first project (AspNet.Identity.MySQL).
Long story short,
I have this and don´t know how to deploy/publish it via File-System:
IdentityWeb is the ASP.NET MVC Appliaction.
Thanks for your help,
Beardy Bear
The first project AspNet.Identity.MySql is a class library, so it's output is a DLL only. When you build the solution, a DLL is output into the bin folder. If project IdentityWeb has a reference to the other one, the DLL for it will appear in the bin for that project too.
As long as when you publish the IdentityWeb project, it has the DLL AspNet.Identity.MySQL.dll in the bin, then you should be able to deploy your ASP.NET MVC application to IIS.

Categories