✅ How do I abort a long running API request manually?
Hi, I'm having a bit of trouble simply cancelling a long running request on a separate ASP.Net Web API.
I've intergrated the CancellationToken into my endpoint to handle it properly when it's fired from the browser (when the back button is pressed or window is closed)
The browser magically sends this cancellation token to my endpoint if the browser is closed or something. This works great.
How I do this manually? Like if I want a button the user can press on the front-end to cancel the process on the back-end.
I've been looking at this :
https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort
but that's JavaScript of course and I'm not sure how to integrate that.
Thanks!
MDN Web Docs
AbortController: abort() method - Web APIs | MDN
The abort() method of the AbortController interface aborts an asynchronous operation before it has completed.
This is able to abort fetch requests, the consumption of any response bodies, or streams.
11 Replies
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
the token is likely HttpContext.RequestAborted. so if you do a request using fetch() in javascript and abort it, result should be the same
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
didn't know it wired it up to CancellationToken parameter, good to know
@TeBeCo @Sehra Thanks for the input guys.
I'm simply wanting a cancel button on the front end to fire some sort of cancellation request on the backend to abort a long running task. It only currently fires when I close the browser tab or navigate using the back button.
I assumed I'd have to wire up a separate API endpoint to fire internal cancellation tokens within it to get what I was looking for.
I think it might as as simple as what you say.
I was under the assumption that I'd need to call the specific API endpoint I'd initially sent the request to for it to pipe through the above mentioned 'CancellationToken' parameter on the initial endpoint.
Look like this 'RequestAborted()' fires for the entire context which is probably what I need.
I'll try just calling a fetch in the way you suggest. I think I might have overcomplicated it.
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
well, for http2/3 just a stream aborted
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
Thanks for the help I'll give this a go!
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
If you have no further questions, please use /close to mark the forum thread as answered