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
Below the controller and method I'm trying to Post to..
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.
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);