A developer has been working on an ASP.NET Core project for a few months. During that time he had various problems I had to fix. Then he converted his application to the RC2 version that was released a few weeks ago. Here we write down a post listing problems that could be fixed. Hope a few would find it useful.
1. ASP.NET Core : Problems and Fixes
A developer has been working on an ASP.NET Core project for a few months. During that time he had
various problems I had to fix. Then he converted his application to the RC2 version that was released a
few weeks ago. Here we write down a post listing problems that could be fixed. Hope a few would find
it useful.
Problem in beginning your app:
You could create your app, but get an exception when starting it:
1) An unsolved exception of type 'System.TypeLoadException' occurred in Program.exe Additional
information: Method 'get_Sources' in type
2) An unsolved exception of type 'System.TypeLoadException' occurred in Program.exe
3) Additional information: Method 'get_Sources' in type
Solution:
Need to check if all packages are rc2-final – you might have an old rc1 reference hidden unknowingly
Run .net restore –no cache
Remove configuration packages from %userprofile%.nugetpackages and restore .net again.
2. While adding ‘old’ class library project as a reference:
Visual Studio(VS) still has problems adding such references to asp.net core projects and gives an error
message. This is how you to fix this:
1. Close VS
2. Erase all wrap folders in the solution
3. Erase all global.json files
4. Delete temporarily all sln files in subfolders of the solution
5. Call dnu wrap …csproj for all dependency projects
6. Restore dnu
7. Open VS again.
Visual Studio (VS) doesn’t see mentioned class library project
If VS 2015 doesn’t see class library project you have mentioned, but developing works just fine. It may
be a problem with project.lock.json files:
1. close VS
2. Erase all project.lock.json files in the solution
3. open VS
EntityFramework Core migrations in Class Library projects:
EF Core preview1 tools don't support running migrations from class library projects. But there is no fix
for that problem. You need to develop a temporary application project, where you can copy your db
context and model classes and run your migration commands.
Return 401 Unauthorized from ASP.NET Core API
Another quick fix while working with ASP.NET Core API today. When you use ASP.NET Core Identity
framework to authenticate users, you probably use [Authorize] feature in your controllers . For those
who don’t know it, you can attach it to a method in the controller if you want this method to be easily
accessed only by users who could successfully log in:
[Authorize]
public IActionResult Index()
{
return View();
}
3. When a user who is not authenticated, tries to access this route, ASP would redirect him to
“/Account/Login”, which is a default login route. You can alter this route in the Startup class, in the
identity options:
services.AddIdentity<User, IdentityRole>(options =>
{
options.Cookies.ApplicationCookie.LoginPath = "/login";
});
This is great for routes returning views.
Generally, in the API, you want is to return an HTTP Status – in this case, “401 Unauthorized”
message.
There are two ways for it. One is via Middleware,and another, a nicer solution id the one that overrides
Identity behavior.
Identity options, apart from setting LoginPath, allows to modify few events; for example;
OnRedirectToLogin:
options.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents
{
OnRedirectToLogin = ctx =>
{
if (ctx.Request.Path.StartsWithSegments("/api") &&
ctx.Response.StatusCode == (int) HttpStatusCode.OK)
{
ctx.Response.StatusCode = (int) HttpStatusCode.Unauthorized;
}
else
{
ctx.Response.Redirect(ctx.RedirectUri);
4. }
return Task.FromResult(0);
}
};
If you insert the above code into your Startup class, Identity will redirect all normal requests to your
login page, but for the API; it will return 401 Unauthorized status code.
Hope you liked this post and interested in ASP.NET Core.
We will be coming up more of such tips in future.
If you want to optimize your skill set in ASP.Net and perfect yourself in .NET training, then our CRB
Tech Solutions would be of great support for you. Join us in our updated program in ASP.Net course.
Stay tuned to CRB Tech reviews for more topics and discussions.