Discovering Generic Controllers in ASP.NET Core

Short Answer Implement IApplicationFeatureProvider<ControllerFeature>. Question and Answer Does anyone know what “service” interface is responsible for [discovering all available controllers]? The ControllerFeatureProvider is responsible for that. And does anyone know of a way to make ASP.NET Core “dump” the names of all the controllers it discovered? Do that within ControllerFeatureProvider.IsController(TypeInfo typeInfo). Example MyControllerFeatureProvider.cs using System; … Read more

Force CamelCase on ASP.NET WebAPI Per Controller

Thanks to @KiranChalla I was able to achieve this easier than I thought. Here is the pretty simple class I created: using System; using System.Linq; using System.Web.Http.Controllers; using System.Net.Http.Formatting; using Newtonsoft.Json.Serialization; public class CamelCaseControllerConfigAttribute : Attribute, IControllerConfiguration { public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor) { var formatter = controllerSettings.Formatters.OfType<JsonMediaTypeFormatter>().Single(); controllerSettings.Formatters.Remove(formatter); formatter = new JsonMediaTypeFormatter { … Read more

How to Customize ASP.NET Web API AuthorizeAttribute for Unusual Requirements

The best solution for my scenario appears to be bypass the base OnAuthorization completely. Since I have to authenticate each time cookies and caching the principle are not of much use. So here is the solution I came up with: public override void OnAuthorization(HttpActionContext actionContext) { string username; string password; if (GetUserNameAndPassword(actionContext, out username, out … Read more

How to extend IdentityUser with custom property

If you follow all steps of adding a custom field to user, you will finish the tasks successfully. Here is all steps to add a custom field to user: Create an ASP.NET Web Application Make sure you select MVC and the Authentication is Individual User Accounts Go to Models folder → Open IdentityModels.cs → ApplicationUser … Read more

TempData null in asp.net core

Edited: As Described in https://learn.microsoft.com/en-us/aspnet/core/security/gdpr?view=aspnetcore-2.2#tempdata-provider-and-session-state-cookies-arent-essential by default TempData cookies are removed by the CookiePolicy Middleware. this can be changed by putting this in Startup.ConfigureServices(): // The TempData provider cookie is not essential. Make it essential // so TempData is functional when tracking is disabled. services.Configure<CookieTempDataProviderOptions>(options => { options.Cookie.IsEssential = true; }); ============================================= Old Answer: After … Read more

How to get POST data in WebAPI?

From answer in this question: How to get Json Post Values with asp.net webapi Autoparse using parameter binding; note that the dynamic is made up of JToken, hence the .Value accessor. public void Post([FromBody]dynamic value) { var x = value.var1.Value; // JToken } Read just like Request.RequestUri.ParseQueryString()[key] public async Task Post() { dynamic obj = … Read more

ModelState.IsValid even when it should not be?

The ModelState.IsValid internally checks the Values.All(modelState => modelState.Errors.Count == 0) expression. Because there was no input the Values collection will be empty so ModelState.IsValid will be true. So you need to explicitly handle this case with: if (user != null && ModelState.IsValid) { } Whether this is a good or bad design decision that if … Read more

MVC5, Web API 2 and Ninject

The Ninject.Web.WebApi NuGet package has just been released. From now on the preferred solution is using that package. I couldn’t find any related documentation but after installing the package everything worked for me. Install-Package Ninject.Web.WebApi After installation both the normal MVC and the API controller instances are provided by Ninject. If you have Ninject.Web.Common already … Read more

Can I incorporate both SignalR and a RESTful API?

Take a look at the video from this blog post. It explains exactly how you can use WebAPI with SignalR. Essentially, Web API + SignalR integration consists in this class: public abstract class ApiControllerWithHub<THub> : ApiController where THub : IHub { Lazy<IHubContext> hub = new Lazy<IHubContext>( () => GlobalHost.ConnectionManager.GetHubContext<THub>() ); protected IHubContext Hub { get … Read more