The tag helper ‘input’ must not have C# in the element’s attribute declaration area

If you don’t need to use the TagHelper, you can use <!elementName> to disable it for a specific element: <!textarea class=”testClass” asp-for=”testId” @readonlyAttribute>@Model.Id</!textarea> See @glenn223’s answer for a more structural solution. I made an improved version of his solution, by adding support for anonymous objects: using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Razor.TagHelpers; namespace {YourBaseNameSpace}.Helpers.TagHelpers { [HtmlTargetElement(Attributes = “custom-attributes”)] … Read more

.NET CORE 3 Upgrade CORS and Json(cycle) XMLHttpRequest Error

The problem was occurring because in .NET Core 3 they change little bit the JSON politics. Json.Net is not longer supported and if you want to used all Json options, you have to download this Nuget: Microsoft.AspNetCore.Mvc.NewtonsoftJson. After that in your Startup.cs file change/fix/add line where you are adding MVC (in the ConfigureServices method. So: … Read more

Error while validating the service descriptor ‘ServiceType: INewsRepository Lifetime: Singleton ImplementationType: NewsRepository’:

Firstly,you need to change: services.AddSingleton<INewsRepository, NewsRepository>(); To: services.AddTransient<INewsRepository, NewsRepository>(); Secondly,you need to inject IMemoryCache instead of MemoryCache in NewsRepository. Here is a simple demo like below: 1.Startup.cs: public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddSession(); services.AddTransient<INewsRepository, NewsRepository>(); services.AddDbContext<BmuContext>(options => options.UseSqlServer(Configuration.GetConnectionString(“Connectionstring”))); services.AddMemoryCache(); } 2.appsettings.json: “ConnectionStrings”: { “Connectionstring”: “Server=(localdb)\\mssqllocaldb;Database=Bmu;Trusted_Connection=True;MultipleActiveResultSets=true” } 3.NewsRepository: public class NewsRepository : INewsRepository { private … Read more

How to resolve HostedService in Controller

Try these two lines in startup.cs: services.AddSingleton<TimedHealthCheckService>(); services.AddHostedService<TimedHealthCheckService>(provider => provider.GetService<TimedHealthCheckService>()); The first line above tells the service provider to create a singleton and give it to anyone who wants a TimedHealthCheckService, like your controller’s constructor. However, the service provider is unaware that the singleton is actually an IHostedService and that you want it to call … Read more

What is equivalent to `MapSpaFallbackRoute` for ASP.NET Core 3.0 endpoints?

In ASP.NET Core 3.0 extension method MapFallbackToController has same functionality to MapSpaFallbackRoute extension method. public void Configure(IApplicationBuilder app) { app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: “default”, pattern: “{controller=Home}/{action=Index}/{id?}”); endpoints.MapFallbackToController(“Index”, “Home”); }); }

Enum type no longer working in .Net core 3.0 FromBody request object

For those who are looking for a snippet when using System.Text.Json public void ConfigureServices(IServiceCollection services) { services.AddControllers().AddJsonOptions(opt => { opt.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); }); } .NET 6 / Top-level statement style using System.Text.Json.Serialization; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers() //convert strings to enums .AddJsonOptions(options => options.JsonSerializerOptions.Converters .Add(new JsonStringEnumConverter()));

How can I get my .NET Core 3 single file app to find the appsettings.json file?

I found an issue on GitHub here titled PublishSingleFile excluding appsettings not working as expected. That pointed to another issue here titled single file publish: AppContext.BaseDirectory doesn’t point to apphost directory In it, a solution was to try Process.GetCurrentProcess().MainModule.FileName The following code configured the application to look at the directory that the single-executable application was … Read more

I am getting “code challenge required” when using IdentityServer4

I am pretty much sure that you are using version 4.0 or above. Let me know if I am correct? In version 4.0 and above, the code flow + PKCE is used by default, as this is more secure than Hybrid flow according to the documentation. Here is the link https://identityserver4.readthedocs.io/en/latest/topics/grant_types.html and link to relevant … Read more

IHostBuilder does not contain a definition for ConfigureWebHostDefaults

Along with using Microsoft.Extensions.Hosting, ConfigureWebHostDefaults also require using Microsoft.AspNetCore.Hosting; as follows: using Microsoft.AspNetCore.Hosting; //<– Here it is using Microsoft.Extensions.Hosting; namespace MyApp { public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IHostBuilder CreateWebHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); } } Moreover it look like your project is … Read more

tech