Angular makes me crazy.
I have two buttons. If I click on the first one, I want to make this request:
https://localhost:44373/api/events
If I click on the second one, I want to make that request:
https://localhost:44373/api/events/1
The method "getNextPost()" will be called and it seems to work, but on Server side the addressed Methods will not be called.
Here is my client implementation:
export class AppComponent implements OnInit {
title = 'EventsPresenter';
_hubconnection : signalR.HubConnection;
_notification : string = '';
displayedColumns: string[] = ['eventDateTime', 'nbr', 'element', 'parent', 'stateTypeTitle', 'enumValueTitle', 'customerObject'];
ROOT_URL = 'https://localhost:44373/';
ROOT_API_URL = this.ROOT_URL + 'api/';
dataSource: Observable<EventData[]>;
dataSource2: Observable<EventData>;
constructor(private http: HttpClient) {}
getPosts(){
this.dataSource = this.http.get<EventData[]>(this.ROOT_API_URL + 'events')
}
getNextPost(){
this.dataSource2 = this.http.get<EventData>(this.ROOT_API_URL + 'events/1')
}
ngOnInit() {
this._hubconnection = new signalR.HubConnectionBuilder()
.configureLogging(signalR.LogLevel.Trace)
.withUrl('https://localhost:44373/notify')
.build();
this._hubconnection
.start()
.then(() => console.log('Connection Started'))
.catch(err => console.log('Error while establishing connection'));
this._hubconnection.on('BroadcastMessage', (data: EventData) => {
console.log(data);
this.dataSource.subscribe(v => v.push(data));
});
}
}
Here is my server implementation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using convisAPI.DataProvider;
using convisAPI.Interfaces;
using EntityLibrary;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
namespace convisAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EventsController : ControllerBase
{
//EventDataProvider eventDataProvider;
IEventDataRepository _eventDataRepository;
IHubContext<NotifyHub> _hubContext;
public EventsController(IEventDataRepository eventDataRepository, IHubContext<NotifyHub> hubContext)
{
_eventDataRepository = eventDataRepository;
_hubContext = hubContext;
}
// GET api/events
[HttpGet]
public async Task<ActionResult<IEnumerable<EventSummary>>> Get()
{
return await _eventDataRepository.GetEvents();
}
// GET api/values/5
[HttpGet("{id}")]
public async Task<ActionResult<EventSummary>> Get(int id)
{
Random r = new Random();
var ra = r.Next(212, 220);
await _hubContext.Clients.All.SendAsync("BroadcastMessage", new EventSummary()
{
Element = "Mein Element " + ra,
Details = "Das ist mein Eventgrund",
EventID = Guid.NewGuid(),
ElementID = Guid.NewGuid(),
EventDateTime = DateTime.Now,
Nbr = ra,
StateNbr = ra,
EnumValueTitle = "Störung",
StateEnumValue = 110 + ra
});
return new EventSummary();
}
// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}
// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}
I know that the code "Get(int id)" may look strange for you, but I basically want to trigger an SignalR Notification.
Any ideas?
best regards