.Net 6 IWebHostENvironment returns local path on server - asp.net-core

I'm doing a .Net 6 application and I have a situation where when I deploy to the server, ENV will return a local path and not the server path. It was a hidden bug since it works fine locally.
I found it by surrounding all of the code with a try\catch.
On the server, ENV.ContentRootPath returns
C:\home\site\wwwroot\
That is the path I can see on the server. Why is .Net WebHost returning the local path? I saw another one with the same issue without a resolution. I'm trying to grab an HTML template for an email.
This si the other issue.
Net Core IWebHostEnvironment getting only "c:\"

Related

appsettings.{ENVIRONMENT}.json not transforming - Deploying asp.net 5 web app to IIS

In my publish profile for staging (/Properties/PublishProfiles/xxx.pubxml) I have:
<EnvironmentName>Staging</EnvironmentName>
I have a file called appsettings.Staging.json.
When I deploy to the server the file stays as appsettings.Staging.json and doesn't get turned into appsettings.json, as I would expect.
Am I missing a step, shouldn't the transformation take place automatically.
The website doesn't work with appsettings.Staging.json file there.
In web.config on the server there is:
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Staging" />
which was added because of the line I added to the publish profile.
Ok, there is no issue here, I mis-understood how it works.
The website wasn't working because in my DAL (Data Access Library) class library it was looking for a file called appsettings.json so it could get the connection string for EF core, which didn't exist. I changed it to look for appsettings.{environment}.json by getting the ASPNETCORE_ENVIRONMENT environment variable at runtime and it works now.

Deploying Asp.net Core 3.1 does not work on Windows server

I have this Asp.net Core 3.1 developed on VS2019 preview.
basically it is a blazor server-side app with referenced assemblies and some nugets.
it runs fine on VS2019 and on my IIS after publishing to folder..
problem is when i move that folder to windows server machine with IIS installed and everything.
an error with 500 code appears.
one thing i noticed here..the log_error.txt file in the server it points out an exception
,and that exception -what ever it is- showing the error line of the startup.cs file with my laptop directory path !
i mean it shows this C:/mrmeeez/..../Startup.cs line 411
but this path does not exists in the windows server machine
it supposed to be F:/.... somthing/Startup.cs line 411
i don't know if it has something too do with the problem anyway..or maybe my assemblies and nugets not shipping correctly to the server
windows server machine is working fine , i tried blazor app template and it ran perfectly.
i need your help guys , Thanks !
Ok , i found it
i don't know for sure is this related to asp.net core 3.1
because i deployed asp.net core 2.1 months ago and it was fine
anyway i used the development exception page in the final publish to see what causing the error and showed this
UnauthorizedAccessException: Access to the path 'C:\Windows\system32\config\systemprofile\AppData\Local\ASP.NET\DataProtection-Keys' is denied.
and after searching for that i found the answer from a guy in github
it seems that the user IIS_IUSRS for this application pool -by default- does not have permissions to access that path
to solve this problem,either one of the two below:
1-Give IIS_IUSRS permissions to access the system files -NOT RECOMANDED-
2-Change the user of the your application pool to LocalSystem account in IIS by going to Application Pools -> Right-Click your app -> Advance Settings -> Process Model section then change Identity
Reference: https://github.com/dotnet/aspnetcore/issues/1983
Thanks!

.NET Core 2.2 API on IIS returns error if it attempts to connect to Db but otherwise works fine

When I publish my .NET core app to a development IIS server, I make a call to the API and the method works fine locally. This method makes a Db call using a connection string stored in
appSettings.json as well as appSettings.Development.json
Observations:
- Yes, I have ASPNETCORE_ENVIRONMENT = Development and I have both an appSettings.json file AND appSettings.Development.json file
- So I started looking at the published files and I had BOTH of these json files in the published folder, even though appSettings.Development.json properties is set to "Build Action" = content and Copy to Output Directory = Do Not Copy
- If I comment out the code that his the Db, and return dummy data, and republish the api, i get the results fine with no complaints about "development mode"
Error I get when calling the API trying to hit the Db
Error.
An error occurred while processing your request.
Request ID:
0HLL1GOCEHH73:00000001
Development Mode
Swapping to the
Development environment displays detailed information about the error that occurred.
The Development environment shouldn't be enabled for deployed applications.
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the
Development environment by setting the
ASPNETCORE_ENVIRONMENT environment variable to
Development
and restarting the app.
Questions:
- The Db connection strings are in both
[ update ]
Brain-fart! The db connection strings were using 'trusted', so no wonder they worked locally! Once I put in the credentials, and re-published, things worked like I expected. However, the error message threw me off.
Im still not sure why I have both of those appSettings files published? Which one will it use?
I am assuming your appsetting files are named as following:
appSettings.json
appSettings.dev.json
typically, you have to explicitly set the environment to dev. if you use Visual Studio for development, it sets an environment variable that tells the application to put it in dev mode.
Without seeing the initializing logic, I would say in prod it will use the appSettings.json.
Take a look at this article, it explains configuration in more details.

'Bad Gateway' error when loading aspnetcore RC2 default website project

I am attempting to run the default ASP.NET Core Web Application (Web Application Template in VS 2015) via IIS.
I have followed the instructions from the docs here eg setting up a 'No Managed Code' application pool and pointing the Physical Path at the location of the web.config file.
When I attempt to browse to the website root, http://localhost I am getting this error:
HTTP Error 502.3 - Bad Gateway There was a connection error while trying to route the request.
Most likely causes: The CGI application did not return a valid set of
HTTP errors. A server acting as a proxy or gateway was unable to
process the request due to an error in a parent gateway.
The event viewer states:
Failed to start process with commandline 'dotnet ./bin/Debug/netcoreapp1.0/WebApplication2.dll', Error Code = '0x80004005'.
This error shows I have replaced the placeholders
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%"
in web.config with the required values
<aspNetCore processPath="dotnet" arguments="./bin/Debug/netcoreapp1.0/WebApplication2.dll"
(update: these placeholders are replaced when you publish)
As per the troubleshooting instructions I have installed .NET Core Windows Server Hosting Bundle and restarted the server.
Running from the command line via dotnet works
Wondering what to try now.
Ok I figured my issue out. I am pointing the physical path in IIS to the unpublished Visual Studio project root directory. This works in previous versions of .net (ie non dotnet core aspnet projects) but doesnt now.
If you really want this to work then you have to publish your app to a temporary folder and copy the Microsoft.AspNetCore.* binaries back into the bin\Debug\ folder along with the refs and runtimes folders
This is a generic error for when IIS can't find the specified .NET Core components ,and doesn't know where to forward requests. In addition to following the official IIS publishing guide, make sure you have:
Installed the correct versions from the download page for latest .NET Core releases
Specified the correct library versions (explicitly or with wildcards) in your project.json
Included a web.config file (preferably using the publish-iis tool), and that the <aspNetCore> key values have been replaced with local values (source, with examples)
I ran into this while setting up a Core 1.0.0 (final) website on a new Windows 2008 Server. Turns out I had mixed up some of the installers from my dev machine with newer versions, and had mismatched library versions (RC2 vs final). Reinstalling the Server Hosting package fixed the problem.
The key to troubleshooting was to get into the console and use the dotnet commands (and resulting error messages) to iterate until I had my site running from the console, since the IIS error page in the browser was useless. I also found some benefit in a few entries in the system Error logs, but not much.

ASP.NET MVC 4 on IIS 7.5, returns 404. Something to do with extensionless route mapping

I have deployed an MVC 4 web-site to a Windows 2008 R2 server with IIS 7.5. .NET 4.5 is installed on the server.
The site works fine locally. All routing works fine. However, when I put it on the server, nothing works. I have confirmed that the DNS/binding is working by putting a static text file in the site and this works.
Here's a few things I've tried:
The default route / returns 403.13 - indicating directory browsing is disabled (correct). This should surely map to /home/index?
Any route (eg. /Organisation, Organisation/Index) simply returns 404.
Made sure all my DLLs are copied over (as per http://haacked.com/archive/2008/11/03/bin-deploy-aspnetmvc.aspx)
I have tried/confirmed:
HttpRedirection is installed on IIS7 (as per http://support.microsoft.com/kb/2023146)
HttpErrors is installed on IIS7
All my DLLs are in place
Am I missing something? My next act of desperation is to install the full ASP.NET MVC 4 stack including Visual Studio Express, SQL Express, etc. just to see if it works. Which I really don't want to do. This is a production server!
Update:
I should add that the same site works on en equivalent configuration in-house, on a staging server.
I had this same issue. There's a hotfix you should try:
http://support.microsoft.com/kb/980368
It fixed the issue for me.