TooManyRequestsObjectResult Class
Definition
- Namespace
- Cuemon.AspNetCore.Mvc
- Assemblies
- Cuemon.AspNetCore.Mvc.dll
An ObjectResult that when executed will produce a Too Many Requests (429) response.
public class TooManyRequestsObjectResult : ObjectResult, IStatusCodeActionResult, IActionResult
- Inheritance
-
TooManyRequestsObjectResult
- Implements
- Inherited Members
Examples
The following example requires Cuemon.AspNetCore.Mvc and shows how to return a from RateLimitController.GetData() and execute it against an MVC ActionContext. It sets up a ServiceCollection with logging, controllers, and DefaultJsonTypeInfoResolver, then calls ExecuteResultAsync with a MemoryStream response body. As the outcome, the result reports status 429 and writes the serialized JSON payload to the HTTP response.
// Program.cs
using System;
using System.IO;
using System.Text.Json.Serialization.Metadata;
using System.Threading.Tasks;
using Cuemon.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using MyApp.Examples;
var result = (TooManyRequestsObjectResult)new RateLimitController().GetData();
Console.WriteLine($"Status code: {result.StatusCode}");
Console.WriteLine($"Value: {result.Value}");
var services = new ServiceCollection();
services.AddLogging();
services.AddControllers().AddJsonOptions(o =>
o.JsonSerializerOptions.TypeInfoResolver = new DefaultJsonTypeInfoResolver());
using var provider = services.BuildServiceProvider();
var httpContext = new DefaultHttpContext { RequestServices = provider };
httpContext.Response.Body = new MemoryStream();
await result.ExecuteResultAsync(new ActionContext(httpContext, new RouteData(), new ActionDescriptor()));
httpContext.Response.Body.Position = 0;
using var reader = new StreamReader(httpContext.Response.Body);
Console.WriteLine($"HTTP status: {httpContext.Response.StatusCode}");
Console.WriteLine($"HTTP body: {await reader.ReadToEndAsync()}");
namespace MyApp.Examples
{
public class RateLimitController : Controller
{
[HttpGet("/api/rate-limited")]
public IActionResult GetData()
{
return new TooManyRequestsObjectResult(new
{
error = "API rate limit exceeded. Please try again later.",
retryAfter = 60
});
}
}
}
Constructors
| Name | Description |
|---|---|
| TooManyRequestsObjectResult(object) | Initializes a new instance of the TooManyRequestsObjectResult class. |