SlideShare a Scribd company logo
1 of 4
Download to read offline
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.
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();
}
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);
}
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.

More Related Content

What's hot

LearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLLearningMVCWithLINQToSQL
LearningMVCWithLINQToSQL
Akhil Mittal
 
Qtp 9.2 tutorials
Qtp 9.2 tutorialsQtp 9.2 tutorials
Qtp 9.2 tutorials
medsherb
 
It Launch Plan
It Launch PlanIt Launch Plan
It Launch Plan
tcaesar
 

What's hot (20)

Murach : How to develop a single-page MVC web
Murach : How to develop a single-page MVC web Murach : How to develop a single-page MVC web
Murach : How to develop a single-page MVC web
 
App c
App cApp c
App c
 
Murach: An introduction to web programming with ASP.NET Core MVC
Murach: An introduction to web programming with ASP.NET Core MVCMurach: An introduction to web programming with ASP.NET Core MVC
Murach: An introduction to web programming with ASP.NET Core MVC
 
Struts notes
Struts notesStruts notes
Struts notes
 
LearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLLearningMVCWithLINQToSQL
LearningMVCWithLINQToSQL
 
Murach: How to transfer data from controllers
Murach: How to transfer data from controllersMurach: How to transfer data from controllers
Murach: How to transfer data from controllers
 
Automation Testing with KATALON Cucumber BDD
Automation Testing with KATALON Cucumber BDDAutomation Testing with KATALON Cucumber BDD
Automation Testing with KATALON Cucumber BDD
 
Inversion of Control and Dependency Injection
Inversion of Control and Dependency InjectionInversion of Control and Dependency Injection
Inversion of Control and Dependency Injection
 
Automation Testing of Shadow DOM Elements with Katalon Studio
Automation Testing of Shadow DOM Elements with Katalon StudioAutomation Testing of Shadow DOM Elements with Katalon Studio
Automation Testing of Shadow DOM Elements with Katalon Studio
 
Struts notes
Struts notesStruts notes
Struts notes
 
Qtp 9.2 tutorials
Qtp 9.2 tutorialsQtp 9.2 tutorials
Qtp 9.2 tutorials
 
Les24
Les24Les24
Les24
 
Exploring Maven SVN GIT
Exploring Maven SVN GITExploring Maven SVN GIT
Exploring Maven SVN GIT
 
Connection management
Connection managementConnection management
Connection management
 
Dependency injection and inversion
Dependency injection and inversionDependency injection and inversion
Dependency injection and inversion
 
Struts,Jsp,Servlet
Struts,Jsp,ServletStruts,Jsp,Servlet
Struts,Jsp,Servlet
 
My Very First Zf App Part One
My Very First Zf App   Part OneMy Very First Zf App   Part One
My Very First Zf App Part One
 
It Launch Plan
It Launch PlanIt Launch Plan
It Launch Plan
 
Introduction to ASP.NET MVC
Introduction to ASP.NET MVCIntroduction to ASP.NET MVC
Introduction to ASP.NET MVC
 
Overview of CSharp MVC3 and EF4
Overview of CSharp MVC3 and EF4Overview of CSharp MVC3 and EF4
Overview of CSharp MVC3 and EF4
 

Similar to Asp dot-net core problems and fixes

.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfolio
mwillmer
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
Nicole Gomez
 
Learning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFrameworkLearning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFramework
Akhil Mittal
 
Diving into VS 2015 Day2
Diving into VS 2015 Day2Diving into VS 2015 Day2
Diving into VS 2015 Day2
Akhil Mittal
 

Similar to Asp dot-net core problems and fixes (20)

ASP.NET Identity
ASP.NET IdentityASP.NET Identity
ASP.NET Identity
 
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Enterprise Level Application Architecture with Web APIs using Entity Framewor...Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
 
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfolio
 
We continue checking Microsoft projects: analysis of PowerShell
We continue checking Microsoft projects: analysis of PowerShellWe continue checking Microsoft projects: analysis of PowerShell
We continue checking Microsoft projects: analysis of PowerShell
 
Inversion of control using dependency injection in Web APIs using Unity Conta...
Inversion of control using dependency injection in Web APIs using Unity Conta...Inversion of control using dependency injection in Web APIs using Unity Conta...
Inversion of control using dependency injection in Web APIs using Unity Conta...
 
Module 4: Introduction to ASP.NET 3.5 (Material)
Module 4: Introduction to ASP.NET 3.5 (Material)Module 4: Introduction to ASP.NET 3.5 (Material)
Module 4: Introduction to ASP.NET 3.5 (Material)
 
Notepad tutorial
Notepad tutorialNotepad tutorial
Notepad tutorial
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
 
Cognos Software Development Kit
Cognos Software Development KitCognos Software Development Kit
Cognos Software Development Kit
 
Resolve dependency of dependencies using Inversion of Control and dependency ...
Resolve dependency of dependencies using Inversion of Control and dependency ...Resolve dependency of dependencies using Inversion of Control and dependency ...
Resolve dependency of dependencies using Inversion of Control and dependency ...
 
Building richwebapplicationsusingasp
Building richwebapplicationsusingaspBuilding richwebapplicationsusingasp
Building richwebapplicationsusingasp
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
 
Learning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFrameworkLearning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFramework
 
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOpsPVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps
 
os-php-wiki5-a4
os-php-wiki5-a4os-php-wiki5-a4
os-php-wiki5-a4
 
os-php-wiki5-a4
os-php-wiki5-a4os-php-wiki5-a4
os-php-wiki5-a4
 
Background Tasks with Worker Service
Background Tasks with Worker ServiceBackground Tasks with Worker Service
Background Tasks with Worker Service
 
Diving into VS 2015 Day2
Diving into VS 2015 Day2Diving into VS 2015 Day2
Diving into VS 2015 Day2
 
Multi-tenancy with Rails
Multi-tenancy with RailsMulti-tenancy with Rails
Multi-tenancy with Rails
 
Aspnet mvc tutorial_01_cs
Aspnet mvc tutorial_01_csAspnet mvc tutorial_01_cs
Aspnet mvc tutorial_01_cs
 

More from sonia merchant

More from sonia merchant (20)

What does dot net hold for 2016?
What does dot net hold for 2016?What does dot net hold for 2016?
What does dot net hold for 2016?
 
What does .net hold for 2016?
What does .net hold for 2016?What does .net hold for 2016?
What does .net hold for 2016?
 
Data protection api's in asp dot net
Data protection api's in asp dot netData protection api's in asp dot net
Data protection api's in asp dot net
 
Authorization p iv
Authorization p ivAuthorization p iv
Authorization p iv
 
Authorization iii
Authorization iiiAuthorization iii
Authorization iii
 
Authorization in asp dot net part 2
Authorization in asp dot net part 2Authorization in asp dot net part 2
Authorization in asp dot net part 2
 
Search page-with-elasticsearch-and-dot-net
Search page-with-elasticsearch-and-dot-netSearch page-with-elasticsearch-and-dot-net
Search page-with-elasticsearch-and-dot-net
 
Build a-search-page-with-elastic search-and-dot-net
Build a-search-page-with-elastic search-and-dot-netBuild a-search-page-with-elastic search-and-dot-net
Build a-search-page-with-elastic search-and-dot-net
 
How to optimize asp dot-net application
How to optimize asp dot-net applicationHow to optimize asp dot-net application
How to optimize asp dot-net application
 
How to optimize asp dot net application ?
How to optimize asp dot net application ?How to optimize asp dot net application ?
How to optimize asp dot net application ?
 
10 things to remember
10 things to remember10 things to remember
10 things to remember
 
Learn dot net attributes
Learn dot net attributesLearn dot net attributes
Learn dot net attributes
 
Learn about dot net attributes
Learn about dot net attributesLearn about dot net attributes
Learn about dot net attributes
 
Owin and-katana-overview
Owin and-katana-overviewOwin and-katana-overview
Owin and-katana-overview
 
Top 15-asp-dot-net-interview-questions-and-answers
Top 15-asp-dot-net-interview-questions-and-answersTop 15-asp-dot-net-interview-questions-and-answers
Top 15-asp-dot-net-interview-questions-and-answers
 
Next generation asp.net v next
Next generation asp.net v nextNext generation asp.net v next
Next generation asp.net v next
 
Dot net universal apps
Dot net universal appsDot net universal apps
Dot net universal apps
 
Browser frame building with c# and vb dot net
Browser frame building  with c# and vb dot netBrowser frame building  with c# and vb dot net
Browser frame building with c# and vb dot net
 
A simplest-way-to-reconstruct-.net-framework
A simplest-way-to-reconstruct-.net-frameworkA simplest-way-to-reconstruct-.net-framework
A simplest-way-to-reconstruct-.net-framework
 
Silverlight versions-features
Silverlight versions-featuresSilverlight versions-features
Silverlight versions-features
 

Recently uploaded

Recently uploaded (20)

COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 

Asp dot-net core problems and fixes

  • 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.