How to acces web api data if I have web api endpoint? Sorry for bad start, I'll try be better

Viewed 48

I have to create Razor Page witch shows items from my web api endpoint link. I have watched hours of tutorials, I know I have to use get method but I am just stuck. It is frustrating because I have figured out everithing else for my project. I am using Visual studio 2022 comunity and net core 6., MSSMS.

Edit: So I have resolved how to get my data from web api with HttpClient, but now my data can not be convertet to my model. I got this error: The JSON value could not be converted to BlazorApp1.Model.CarDealerModel. Path: $ | LineNumber: 0 | BytePositionInLine: 1.

Here is code for model:Model

Code for Razor:codeForRazor1

codeForRazor2

Codefor Prgram.csprogram.cs

1 Answers

You can call your web Api endpoint from your asp.net core 6 Application easily.

From VS2022 create your main solution and after taht create the two projects for you Web API Project and also for the ASP.Net core one.

  • VS2022 provide template projects for each one and also when you add your API controller you can create one with all the actions(GET/POST/PUT...) generated by VS.

Other side for Asp.net project they are the scaffold Controller that generate also the Controller/Model and the View for you controller.

By the end, to make a connection betweeen the Asp.net controller and the Web API one you must use the restsharp nuget package within your Asp.net project and call the Get endpoint from you WebAPI project.

Hope this will give you an idea to how to implemente it and fill free if you have more questions.

Related