Blazor Redirect between api controllers in same project

labjac

Member
Joined
May 26, 2022
Messages
9
Programming Experience
1-3
Hallo

I want to redirect in a blazor server side project between api's, can use RedirectToRoute("Controller") but this will only call the GET method.

I need to Post from one main controller to another, I got one main controller which validate and decide which controller needs to get the message. But the api controllers is all in the same project, below is the only way I can get this to work, but I don't want to use absoluteUrl, this is causing issues when the server name or port numbers change when it's deployed.

I've tried various methods but cannot find a proper way of getting the absoluteUrl from the controller.

Below the controller I'm trying to call from

Section of the main controller:
case "WCSTSCORD":
                    {
                        string messageType = "WCSTSCORD";

                        var dsvWcsTscord0100_Model = WcsTsCord0100_Parser.DSVWCSTSCORD0100ToModel(cargoWavePost_Api);

                        var serialDatasend = JsonSerializer.Serialize(dsvWcsTscord0100_Model);

                        //This will write to the API routing the WCSTSCRCORD message.
                        try
                        {
                            var response = await _http.PostAsJsonAsync($"https://{ApplicationVariables.Hostname}:{ApplicationVariables.ApiPortNr}/api/DSVWCSTSCORD0100_Inbound", dsvWcsTscord0100_Model);
                            response.EnsureSuccessStatusCode(); // throws exception if status code is not successful
                            return Ok($"{messageType} routed successfully");

Below the controller and method I'm trying to Post to..

Controller and action to perform:
namespace GDE.Sorter.Api.Server.Controllers.Loggers
{
    [Route("api/[controller]")]
    [ApiController]
    public class LogJsonRawMessagesController : ControllerBase
    {
        private readonly DataContext _context;
        
...............

[HttpPost]
        public async Task<ActionResult<LogJsonRawMessage>> PostLogJsonRawMessage(LogJsonRawMessage logJsonRawMessage)
        {
            if (_context.LogJsonRawMessage == null)
            {
                return Problem("Entity set 'DataContext.LogJsonRawMessage'  is null.");
            }
            _context.LogJsonRawMessage.Add(logJsonRawMessage);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetLogJsonRawMessage", new { id = logJsonRawMessage.Id }, logJsonRawMessage);
        }

I've create two variable in the appsettings.json which is used to change the Hostname and PortNumber when deploying, sure there should be a better way.

I've tried using NavigationManager and the UrlHelper classes, but all returns null.

Below some idea what I'm trying to achieve and what I've tried. (The UrlHelper is already injected into the controller and service loaded in the Program.cs.

Roouting attempts:
            var requested = new Uri(_urlHelper.Link("/api/LogJsonRawMessages");

            var req1 = new Uri(_urlHelper.RouteUrl("/api/LogJsonRawMessages"))

            var urlnew = _urlHelper.Action("/api/LogJsonRawMessages",""

            var controller = nameof(LogJsonRawMessagesController);
            var action = nameof(LogJsonRawMessagesController.PostLogJsonRawMessage);           
            var requestUrl = Redirect.UrlHelper("")("/api/LogJsonRawMessages").ToString();
            var responseLog = await _http.PostAsJsonAsync(requestUrl, logJsonRawMessage);
 
Config is the way I'd do it; presumably your BSS app is one project and api controllers another, so some simple config tells the BSS app where its backing API(s) is(are). One day they may not be on the same machine, so having hard coded the BSS app to find the api locally is a headache. If they're the same app and will not be separated, I understand also, but in that case I wouldn't be getting c# to make an http call essentially to call another method that is reachable within code
 
I wouldn't be getting c# to make an http call essentially to call another method that is reachable within code
+100
 
Solution
Back
Top Bottom