Deploying dotnetcore 5 web api to iis gives 404 - .net-core

I have an Web API project with .NET 5 (created directly from the template of visual studio Asp.NET Core Web API), it works just fine when debugging from Visual studio, tried to deploy it to IIS server, which has the Hosting Bundle of .NET 5 installed, the deployment apparently runs fine, but when I enter the url I have a 404. Have a separate MVC .NET 5 project, which I deploy in the same way and it runs perfectly fine.
Would like to know if someone can point me to the right direction, can't find what I'm missing here.

After searching and messing with Startup.cs file I notice that
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "api v1"));
Was inside the validation if(env.IsDevelopment()), but the api endpoints were working just fine, just that though that it doesn't work because I was unable to see the Swagger site, probably because you don't want to expose your api to everyone... feels like I miss a little explanation somewhere but all good now.

Faced the same issue. After configuring, when tried to browse 404 error came up.
The reason is, Swagger page will be configured in only for development environment as below(in Startup.cs).
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "AppName v1"));
}
The APIs will be available if we type in the correct url.
If you still wants to view the Swagger file
Either change the code in startup.cs remove the check for
if (env.IsDevelopment())
Or In your web.config file set environment as Development.

If you have deployed a NET5 API to local IIS for development propose, you should set the environment to development using the web.config file
<aspNetCore processPath="dotnet" arguments=".\NewDealerWebAPI.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" >
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore

You can solve your problem by following the steps on this site.
https://learn.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-3.1&tabs=visual-studio

You didnt specify where you're pointing to swagger or directly to the api?
Additional things to check with OpenApi/Swagger/Swashbuckle/whatever the name is this week:
If you're going to https://yourAppUri/swagger/index.html
As noted in other replies, if you deploy to production, the environment may be different so the "if (env.IsDevelopment())..." is in your program.cs or startup.cs and your server has the "Production" or "Staging" value set, you'll get a 404 because swagger isn't found or generated because of that if statement.
There is a hierarchy to where the env vars are set if you're unsure. Basically its in this order: command line, environment variables, user secrets, appsettings.someEnvironment.json, then finally appsettings.json.
Verify that in your .csproj file it contains the following in the PropertyGroup tag (I had recently generated a new api project via the cli and the below was conveniently left out:
...
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
For internal APIs, I completely remove the if statement to always generate the swagger docs so end users can use it, but also force an api key so its semi locked down.
For hitting the api (since there's no controller code in your question):
Validate your url is correct, I'd copy the url after visual studio or rider starts up with and replace localhost with your server name/info and hit it with PostMan or something similar
Verify you dont have other bindings set in IIS, is the host environment using different ports to go to like: 8080.
Most other things would throw a 500 error I can think of...

Related

aspnet core deployment on production machine is looking for appsettings.json using development machine path

Following the Microsoft docs I used VS 2019 to publish an Asp Net Core app.
Following the Microsoft docs to deploy an app on IIS.
I have the correct ASP.NET Core Module and Runtime (3.1) installed for my project.
When navigating to the app's home (any) page, I get "This site can't be reached" response page. This is not an error page from IIS on my VPS, it seems to be a default page / message from the browser.
There are no error messages from IIS events on my VPS.
To my understanding, Windows Process Activation Service (WAS) isn't starting the application, though the application pool shows 1 app assigned to it.
So using the web.config, I set logging to true.
<aspNetCore processPath=".\IS4.exe" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
I then went into the project's physical directory on the VPS, right-clicked on IS4.exe and ran as administrator. That resulted in an error that got logged.
Here's a screen shot
So my app's program.cs is being passed an empty string instead of a URI. But it is using the path from my development laptop; NOT the path from set in the web.config's environment variables on the production machine.
THERE ARE NO HARDCODED URLs / URIs at all in the project. All variables requiring a URL / URI are set in the appsettings.json and in environment variables. EG: Configuration["AppURLS:IS4BaseUrl"]
So why doesn't line 12 and line 15 of the log file say:
blah blah blah ....................in C:\inetpub\IS4\Program.cs:line 81
and
at IS4.Program.Main(String[] args) in C:\inetpub\IS4\Program.cs:line 58
I haven't found a good google search term or phrase that leads me to an answer, so I thought to ask here.
Why is the program.cs logging out my development environment's project directory and not the directory of the server that I am trying to deploy it on. I think that is the root of my deployment problems, or at least a starting point.
Anybody have any ideas? Thx
So I am still not sure why the error log pointed to my laptop directory rather than the deployment server's directory, but I noticed that my db connection string wasn't properly escaped. Once I fixed the connection string the app started and I was able to navigate to the app and browse the site.
Spelling and escaping strings are my bane :(

ASP.NET End point 404 on IIS Server

Been using Visual Studio 2019 & ASP.Net. Got all my endpoints working during the development. When deployed on IIS, all my endpoints return 404. While looking at the logs, I get a "404 0 2" error.
I can browse to all pages fine, just the end points not working. I have tried adding various things to my Web.Config all with the same result.
Is a 2019 Server with the latest version of IIS.
Any help is appreciated. Guessing something is setup wrong on IIS?
The endpoints are done in using a controller, sample code below:
[Route("~/api/students/getyear/{yearid}")]
public async Task<ActionResult<IEnumerable<Student>>> GetYear(int yearid)
{
return await _context.Student.Where(x => x.yearid == yearid).ToListAsync();
}
Called by using
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
When debugging it, I can reach it from "https://localhost:5001/api/students/getyear/0" and when trying on my IIS using "intranet.domain.co.uk/studentnotices/api/students/getyear/0" I get the 404 error
I publish the code to the release folder and copy it to my IIS. Have also tried publishing directly to the IIS but same result
The win32-staus has already indicated that "The system cannot find the file specified."
So I think your IIS is trying to execute the URL from physical path.
It might because your static file handler is taking over the request instead of asp.net extensionless handler.
Please install IIS asp.net extension feature in turn windows features on or off. For .net core ,application, please ensure asp.net core web hosting bundle has been installed.

ASP.NET WEB API 2 Works Locally But Not On Production Web Server

I've developed a new WEB API 2 that works great locally, however when I upload the same code to my production server (Arvixe in this case) all I get is a 404 when I call it. I've spent HOURS searching the web, reading forums, etc.. and have been able to find no resolution, so I'm asking here as my last effort.
I'm currently only testing with the default project that gets created when you do New Project > ASP.NET Web API 2 Empty Project in Visual Studio. This creates an empty project with a single ValuesController. You should be able the JSON response by called /api/values, but this doesn't even work.
I'm using Fiddler to test the API locally and on the web server.
http://localhost:1993/api/values <--- works great
but
http://api.mydomain.com/api/values <--- returns 404
Note: I created a subdomain "api" in this case, but everything for the code for the API is unchanged from when it was created.
Why in the world does this work locally but not on the production web server?
That the server returns 404 (Not Found) may indicate a lot of things. However you can check using the following step:
Add a simple text document like readme.txt to your a folder sub-domain http://api.mydomain.com, and try to get access to that. If you can't access to that file, it means that the subdomain is not configured properly.
Publish the webservice using the "Publish" functionality, so that all DLLs will be copied.
After that,try to reach the Web ApI again.
Hope that help.
"Note: I created a subdomain 'api' in this case, but everything for the code for the API is unchanged from when it was created."
Above comment of your's is suspicious, you should publish your WEB API application in the root directory. Like if http://example.com is pointing to "MyExample" folder, then application should be published on "MyExample" folder.
After that you will be able access your api with http://example.com/api/{controller}/{action}
Just a simple suggestion which I'm sure you have already considered, but have you opened the http port 80 on the server's firewall?
Also stick a plain old html file in the root of your project and see if the server serves it up.
in your case, since you create a subdomain of 'api', you should try
http://api.mydomain.com/api/api/values
note that if you're using database for the function, you should change the connectionString in your web config
Please verify the .net framework on you hosted domain that may be old one.
Web api 2 is supposed on 4.5 framework.
One reason for web api 2 method working OK on local machine but not on production server is that the method you are calling is working on local machine but not on remote server. In such a case you will receive message 404 or 500, and you would be lost why this routing is failing.
Why a method would fail on remote server, well there may be many reasons. For me, I was querying database in my method and my connectionString was not set for remote server.
One way of resolving it would be to put some very simple code in that particular method and test that routing is working. Then check your original code for errors reasons.

IIS 7.5 and Razor Site: 500 errors for CSS and JS

For some reason, when I deploy my Razor MVC web site to my Windows 2008 R2 server, I'm getting 500 internal server errors for all CSS and JS. I'm not sure why, because I've done the following:
Enabled static content in IIS
Enabled anonymous access, with the default ID being the application pool identity and given that identity read/write permission to the folders
Ensured my static content handler was setup correctly
What other problems could it be? How can I even debug this to see what the actual error is? Even though I have an Application_Error handler, nothing is getting logged. And IIS logs doesn't give me the error info?
Thanks.
Mime-Types were my trouble
I had the same problem. My css and js was not delivered. Server says internal error.
I found out that i added the mimetype for mp4 in the global settings of the iis and also added the mime type in one of my websites as well.
That was a problem. This mime-type can only exist in the website or global, not both.
I deleted tehe global mimetype and everything worked like it should be.
Hope to help some guys of you.
Just found, that I've added to web.config *.woff MIME for IIS 7.5, so when deploying on IIS8, it's causes static files error. After removing that from web.config everything is fine.
I got 500 errors because I added this below to my web.config. After I removed it, I got passed the error.
<staticContent>
<mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
</staticContent>
To rule out one basic permissions situation, try this:
Select your web site and go into Basic Settings.
Take note of which application pool your site is running under.
Exit out of there and select the Application Pools node.
Find the application pool for your site, and take note of which identity (account) that application pool is running under.
If the account is a specific user identity -- in other words, if it's not a built-in account (such as ApplicationPoolIdentity, Network Service, Local System, etc) then:
Launch your Local Users and Groups.
Find and open the local IIS_IUSRS group.
If the account in question is not a member of the group, add it.
Reinstalling IIS fixed the problem.
If you are on your local server and taking this error, please check if you have any missing packages. Catch errors in global.asax and if neccessary reload all packages with;
Update-Package -reinstall
in Package manager console.
Hope it helps.

Changing "Project Url" causes ASP.Net Web app to not be debuggable

I have an ASP.NET Web Application project that I am using to host a WCF Data Services (OData) project.
I went and changed the url from:
http://localhost/MyProject
to
http://localhost/v1/MyProject
after I did that I created a Virtual Directory for the new project URL.
Now when I run I get this error:
Unable to start debugging on the web server. The web server is not configured correctly. See help for common configuration errors. Running the web page outside of the debugger may provide further information.
I clicked help but it was no help (was IIS 6 level instructions, I have IIS 7). I did some googling and it was all fairly generic responses.
How can I get this working again? (Aside from revert to my old Url. Reverting works but I changed it for a very good reason.)
Your virtual directory need to point to the same scr directory you were originally debugging against.
Also, make sure the virtual directory is configured for Windows Authentication, which is required for debugging.
I had to go to my website in IIS and add a folder under it called v1. After I did that it all worked perfectly.

Resources