Routes in ASP.net Core API

Try this. You can put a common route prefix on the controller. [Route(“api/[controller]”)] public class BXLogsController : Controller { //GET api/BXlogs/id/blah [HttpGet(“ID/{id}”, Name = “GetL”)] public IActionResult GetById(string id) { … } //GET api/BXlogs/api/blahapi [HttpGet(“API/{apiname}”, Name = “GetLAPI”)] public IActionResult GetByAPI(string apiname) { … } } read up on attribute routing here Routing to Controller … Read more

How to enable cors in ASP.NET Core 6.0 Web API project?

Add service builder.Services.AddCors and app add app.UseCors(“corsapp”); replace builder.WithOrigins(“*”) with builder.WithOrigins(“http://localhost:800”, “https://misite.com”); check documentation var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); //services cors builder.Services.AddCors(p => p.AddPolicy(“corsapp”, builder => { builder.WithOrigins(“*”).AllowAnyMethod().AllowAnyHeader(); })); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } … Read more

Async IServiceProvider in .NET Core DI

Although it is theoretically possible to use async/await during object resolution, you should consider the following constraints: Constructors can’t be asynchronous, and Construction of object graphs should be simple, reliable and fast Because of these constraints, it’s best to postpone everything that involves I/O until after the object graph has been constructed. So instead of … Read more

Update/change roles claim (or any other claim) in JWT

Refresh tokens don’t seem to be the solution if you care about the changes you make being instant, you probably don’t want an user to access moderation tools for some time if you revoke his permissions. What you could do is keep a version number in the jwt token relative to the user, much like … Read more

Using a C# 7 tuple in an ASP.NET Core Web API Controller

It doesn’t work because named tuple names are not quite “real”, it’s mostly syntax sugar provided by compiler. If you look at ValueTuple set of types, by which named tuples are represented, you will see that they have properties like Item1, Item2 and so on. Compiler will rewrite all your references to named tuple names … Read more

Read request body twice

If you’re using application/x-www-form-urlencoded or multipart/form-data, you can safely call context.Request.ReadFormAsync() multiple times as it returns a cached instance on subsequent calls. If you’re using a different content type, you’ll have to manually buffer the request and replace the request body by a rewindable stream like MemoryStream. Here’s how you could do using an inline … Read more

The input was not valid .Net Core Web API

Don’t use FromBody. You’re submitting as x-www-form-urlencoded (i.e. standard HTML form post). The FromBody attribute is for JSON/XML. You cannot handle both standard form submits and JSON/XML request bodies from the same action. If you need to request the action both ways, you’ll need two separate endpoints, one with the param decorated with FromBody and … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)