Posts

Showing posts with the label .Net Core

AddEntityFrameworkStores Can Only Be Called With A Role That Derives From IdentityRole In .NET Core 2.0

Answer : Long time since I asked this question, but here's how I deal with nowadays: Startup.cs services.AddIdentity<User, Role>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); services.AddScoped<RoleManager<Role>>(); Entites: public class User : IdentityUser<int> { } public class Role : IdentityRole<int> { } For same issue, you can look at this:https://github.com/aspnet/Identity/issues/1364

ASP Core WebApi Test File Upload Using Postman

Image
Answer : Thanks to @rmjoia's comment I got it working! Here is what I had to do in Postman: The complete solution for uploading file or files is shown below: This action use for uploading multiple files : // Of course this action exist in microsoft docs and you can read it. HttpPost("UploadMultipleFiles")] public async Task<IActionResult> Post(List<IFormFile> files) { long size = files.Sum(f => f.Length); // Full path to file in temp location var filePath = Path.GetTempFileName(); foreach (var formFile in files) { if (formFile.Length > 0) using (var stream = new FileStream(filePath, FileMode.Create)) await formFile.CopyToAsync(stream); } // Process uploaded files return Ok(new { count = files.Count, path = filePath}); } The postman picture shows how you can send files to this endpoint for uploading multiple files: This action use for uploading single file : [Http...

Bootstrap 4 Library Name In Libman (CDNJS) From Visual Studio 2019

Image
Answer : From the CDNJS page: twitter-bootstrap You can use the Provider unpkg

Can I Use HttpClientFactory In A .NET.core App Which Is Not ASP.NET Core?

Answer : According to the documentation HttpClientFactory is a part of .Net Core 2.1, so you don't need ASP.NET to use it. And there some ways to use are described. The easiest way would be to use Microsoft.Extensions.DependencyInjection with AddHttpClient extension method. static void Main(string[] args) { var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider(); var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>(); var client = httpClientFactory.CreateClient(); } Thanks for replies. So it is possible to use in console app. There are a few ways to do this, depending on what way you want to go. Here are 2: Directly add to ServiceCollection e.g. services.AddHttpClient() Use Generic host e.g. Add httpclientFactory in .ConfigureServices() method See here for blog post using in console app As one of the answers suggests, you do not need ASP.NET to use it However, you need a bit of work to ge...

Cannot Find Command 'dotnet Ef'?

Image
Answer : In my case, the tools folder didn't exist inside %USERPROFILE%\.dotnet\ so I had to run the command dotnet tool install --global dotnet-ef to install dotnet ef. Then I was able to run dotnet ef... This was the result of the above install command: Note to readers: If you haven't installed dotnet ef , you need to install it first: dotnet tool install --global dotnet-ef . The question-asker already did that. You need to do that first before the rest of this answer can help. How to fix this For Linux and macOS , add a line to your shell's configuration: bash / zsh : export PATH="$PATH:$HOME/.dotnet/tools/" csh / tcsh : set path = ($path $HOME/.dotnet/tools/) When you start a new shell/terminal (or the next time you log in) dotnet ef should work. For Windows : See this question on how to add to the PATH environment variable. You need to add %USERPROFILE%\.dotnet\tools to the PATH . What's going on? The .NET Core 3.0 (preview) ...

Blazor Project Structure / Best Practices

Image
Answer : I just created a new ASP .NET Core 3.1 project with 3 web apps: MVC, Razor Pages and Blazor. NetLearner: https://github.com/shahedc/NetLearnerApp I’m developing all 3 in parallel so that you can see similar functionality in all of them. I’ve extracted common items into a Shared Library for easy sharing. The shared library includes: Core items (Models and Services) Infrastructure items (Db context and Migrations) Here’s the corresponding blog writeup, which will be followed by an A-Z weekly series, which will explore 26 different topics in the next 6 months. blog post: https://wakeupandcode.com/netlearner-on-asp-net-core-3-1/ Hope the current version is useful for what you’re asking for. Stay tuned and feel free to make suggestions or provide feedback on the project structure. So I was diving into looking for more example projects and I came across a SPA Server Side Dapper application (https://www.c-sharpcorner.com/article/create-a-blazor-server-spa-wit...

Cache Busting Index.html In A .Net Core Angular 5 Website

Answer : Here's what I ended up with after combining a bunch of answers. My goal was to never cache index.html. While I was in there, and since Angular nicely cache-busts the js and css files, I had it cache all other assets for a year. Just make sure you're using a cache-busting mechanism for assets, like images, that you're managing outside of Angular. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { // ... app.UseStaticFiles(); if (env.IsDevelopment()) { // no caching app.UseSpaStaticFiles(); } else { app.UseSpaStaticFiles(new StaticFileOptions { OnPrepareResponse = context => { context.Context.Response.Headers.Add("Cache-Control", "max-age=31536000"); context.Context.Response.Headers.Add("Expires", "31536000"); } }); } // ... app.UseSpa(spa => { spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions ...

Assets File Project.assets.json Not Found. Run A NuGet Package Restore

Answer : To fix this error from Tools > NuGet Package Manager > Package Manager Console simply run: dotnet restore The error occurs because the dotnet cli does not create the all of the required files initially. Doing dotnet restore adds the required files. In my case the error was the GIT repository. It had spaces in the name, making my project unable to restore If this is your issue, just rename the GIT repository when you clone git clone http://Your%20Project%20With%20Spaces newprojectname In case when 'dotnet restore' not works, following steps may help: Visual Studio >> Tools >> Options >> Nuget Manager >> Package Sources Unchecked any third party package sources. Rebuild solution.

AspNetCore.Mvc.Core Version Mismatch

Answer : <PackageReference Include="Microsoft.AspNetCore.App" /> I had the same issue, after add this line to unit test project, it start pick right version of Microsoft.AspNetCore.App. Update The issue noted below has been fixed & you should be able to benefit from implicit package versioning and reference like below without providing the version number of the package. <PackageReference Include="Microsoft.AspNetCore.App" /> Original Answer This issue is because of the Implicit Versioning that was introduced for Microsoft.AspNetCore.App metapackage. With implicit versioning the sdk decides the version & it resolved it as 2.1.1 However, it was resolving to version 2.1 for the nunit test project. Specifying the version number for the nunit project like <PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.1"/> and performing dotnet restore helped resolve this issue. There is a ticket for thi...