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
Related
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.
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.
How do I launch an ASP.NET Core Web-API programmatically from C#? The API should launch after I click on a button in a form.
I already added my API to my solution via Add -> Existing Project... but I don't know how to launch it from a different Project.
The Web-API is running on .NET Core 2.0 and I want to launch it from a project using .NET Framework 4
Probably you are looking for something like this(link)
dotnet run [-c|--configuration] [-f|--framework] [--force] [--launch-profile] [--no-build] [--no-dependencies]
[--no-launch-profile] [--no-restore] [-p|--project] [--runtime] [[--] [application arguments]]
dotnet run [-h|--help]
You can also try to run the application as a Windows Service, and start the service on click.
Here is a sample project showcasing hosting of ASP.NET Core app in WPF: https://github.com/mkArtakMSFT/Samples/tree/master/WpfAspNetCore
Couple of things to pay attention to:
The .NET Framework 4.6.1 is the minimum version which can host ASP.NET Core apps
This sample is dummy and doesn't have much configuration (based off of empty ASP.NET Core Web app template content). You can, however, add reference to the Microsoft.AspNetCore.Mvc package to add support for MVC.
Here's my story: I created a solution in VS2017 which contains 3 projects: .NET Core 2.0 dll to store my models, WebAPI (which uses EF Core 2.0 code first) and blank Web Application. In the blank project I would like to create a frontend Aurelia app. So, I did 'au new --here' on that project, selected TypeScript as my transpiler. Now, I have a solution with 3 projects which doesn't build because of:
Error TS5055 Build:Cannot write file 'XX/karma.conf.js' because it would overwrite input file.
I updated TypeScript to 2.3 by adding a proper entry to .csproj but still cannot compile my solution in VS2017.
My question is: how to add an Aurelia project to .NET Core 2.0 solution and get "au run --watch" works for it? I followed this post: http://mobilemancer.com/2016/10/19/aurelia-spa-typescript-dotnet-core/ but it seems not to consider all possible scenarios.
Please help me get started with .NET Core 2.0 Aurelia + WebAPI as I've been struggling with that for a couple of days.
You can look at the code from Aurelia-Typescript-Skeleton from GitHub https://github.com/aurelia/skeleton-navigation/tree/master/skeleton-typescript-aspnetcore
They have created asp.net core application and basic structure for aurelia and typescript.
You can install SPA Templates from ASP.NET core 2.0 with CLI command dotnet new --install Microsoft.AspNetCore.SpaTemplates::*
There you can find reach set of templates to start with ASP.NET core 2.0
I want to ask for help on how to install EntityFramework on my web api project (.net core).
I tried installing it but it keeps showing the error "Package restore failed. Rolling back packages.."
Is EntityFramework compatible on web api?
Thanks in advance for your response. Have a good day!
If you run a .Net Core Mvc project which is the combine Web Api and Mvc for Core the package Microsoft.AspNetCore.All, which you most likely already have installed, already include this.
The docs says
To add EF Core support to a project, install the database provider
that you want to target. This tutorial uses SQL Server, and the
provider package is Microsoft.EntityFrameworkCore.SqlServer. This
package is included in the Microsoft.AspNetCore.All metapackage, so
you don't have to install it.