Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor web assembly, calling an ASP.NET Core Web API : GET works, but POST does nothing when called from the Blazor client

$
0
0

I am creating an ASP.NET Core Web API that will do CRUD for a Locations entity. I call the API from my Blazor web assembly project, and the GET verb I have in place works (I can list locations from my database in a .razor component page, in a grid), but when I try to do a POST from the same client to the same route, nothing happens. I can 'try it out' in swagger and my POST code works (it inserts the new location), but from the web assembly client, there is no result and the new location is not inserted.

What might I be missing?

Here's my API LocationController:

using Microsoft.AspNetCore.Mvc;using BlazorAppDiscoveryAPI.Models;using Microsoft.Data.SqlClient;using Dapper;using System.Data;namespace BlazorAppDiscoveryAPI.Controllers{    [ApiController]    [Route("api/Locations")]    public class LocationController : ControllerBase    {        static IConfiguration conf = (new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json").Build());        public static string connectionString = conf["ConnectionStrings:Default"].ToString();        // This works, I can display the Locations from the db on my blazor client page.        [HttpGet(Name = "GetLocations")]        public IEnumerable<dynamic> Get()        {            string strSQL = "select * from dbo.Location";            using IDbConnection connection = new SqlConnection(connectionString);            {                return connection.Query(strSQL);            }        }        // This works in swagger, but not when called from the blazor client page. I'm not sure the client call even gets to here.        [HttpPost]        public int Add(LocationModel newLocation)        {            string strSQL = @"INSERT INTO dbo.Location(Location_NUM, Location_OWNER)                            VALUES(@Location_NUM, @Location_OWNER);";            using IDbConnection connection = new SqlConnection(connectionString);            {                return connection.Execute(strSQL, newLocation);                           }        }    }}

Again, this much works in swagger for a GET and POST to localhost.../api/Locations/.

From the client side: I'm calling the API from the Blazor component page:

@code {    private List<LocationModel> Locations;    private LocationModel newLocation = new LocationModel();    protected override async Task OnInitializedAsync()    {        Locations = await LocationService.GetAllLocationsAsync();    }    private async Task InsertLocation()    {        LocationModel s = new LocationModel            {                Location_NUM = newLocation.Location_NUM,                Location_OWNER = newLocation.Location_OWNER            };        await LocationService.InsertLocationAsync(s);        newLocation = new LocationModel();    }}

Here is the LocationService.cs, making the requests:

using BlazorAppDiscovery.Client.Models;using System.Net.Http.Json;using Microsoft.Extensions.Configuration;namespace BlazorAppDiscovery.Client.Services{    public class LocationService(HttpClient http) : ILocationService    {        public static string baseaddress = "https://localhost:7276/"; // TODO: get this from the config file.        public async Task<List<LocationModel>> GetAllLocationsAsync()        {             string requestUri = baseaddress +"api/Locations/";            return await http.GetFromJsonAsync<List<LocationModel>>(requestUri);        }        public async Task InsertLocationAsync(LocationModel newLocation)        {            string requestUri = baseaddress +"api/Locations/";            await http.PostAsJsonAsync(requestUri, newLocation);        }    }}

What else might I have missed so that my GET works perfectly from both the client and swagger, but the POST only works from swagger, not the client?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>