Publish .NETCore 2 API and host on IIS - asp.net

I've created a .net Core 2 API and published it to the server.
The file structure looks very odd compared to a normal .Net MVC structure
Normal Structure I am familiar with
And this is the structure of the deployed .NET CORE 2 API
Now if I just try and convert that folder to an application like a normal ASP.NET app, it doesn't work.
I've installed the .NET Core Windows Server Hosting bundle on the server, but I'm still missing something.
Where do you add this code to enable IISIntegration ?
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();

That code goes in Program.cs, but it's the default, so you should already be fine there.
The ASP.NET Core file structure is different than MVC 5, but it all pretty much works the same, once you've installed the .NET Core Hosting Runtime. You drop the published files in a directory on the web server. Then, you set up a site in IIS to use that directory. The only thing slightly different is that you need to edit the App Pool and set it to "No Managed Code", instead of a particular .NET runtime. Other than that, it should all just work.
That said, if your screenshot is a complete listing of the files, then you do seem to be missing some stuff. It's possible the publishing process failed at some point. Try to republish.
Background
The way ASP.NET Core works is fundamentally different than previous ASP.NET web applications. Whereas you used to have all the HTTP modules and such coming from the monolithic .NET Runtime installed on the machine, ASP.NET Core apps are completely self-contained. A Core app is in fact merely a console application. In an IIS setup, the actual web server is Kestrel, which either bundled into the Core app or available from the .NET Core runtime. IIS works as a reverse proxy. Handing off requests to Kestrel and then returning the responses it gets from Kestrel (hence the "No Managed Code"). With IIS deployment your app is a DLL, but it can also be entirely self-contained and deployed as an executable. The point is that, yes, the file structure and such is very different because it fundamentally works in a different way.

Related

Asp.net website not starting default.aspx (apache 2.4 on Windows 10 Pro; IIS 10)

So, I setup a Win10 box as an Apache server; built VS19 Asp.net webforms app, which works as expected when run in VS. published to the C:\Apache24\htdocs folder. Now, I setup port forwarding on my ISP-provided "Internet IP Address" to point to my local server. If I use any browser (outside of my LAN) to access that address (e.g. http://xx.yy.zz.qq - not showing actual IP for security reasons) I will see the page titled "index of /", and the list of files that I published to that htdocs folder. If I put in INDEX.HTML file in that folder, it will be displayed; but since I see Default.aspx as the 1st entry in the IIS Default Documents list, with NO index.html present, I still just see the list of files, not the aspx. I don't think the fact that I've not yet established a DomainName-to-IP binding should make any difference to this issue, but I can't be sure. Any thoughts? As you might guess, I've not done this before! :) ). TIA!
Jim
I made sure that the VS instance of the website was not running, just in case that might cause IIS confusion; but I don't think that would be the case.
As noted, I can't see how a non .net server going to run/launch/process correctly aspx pages that require IIS as the web server?
I mean, IIS can't run a web site designed for Apache, nor can say IBM "web spere" web server either.
I am trying "hard" to think how a application built in .net framework going to work with a web server that not designed to work with the .net framework?
I mean, if you have a computer, and have a word document, then you need word installed to work with that document.
If you have a PDF file on your computer, then you have to install some PDF software to use that PDF document.
A web server is not some "magic" box, but is a plane jane computer, with some software installed on it. So, if you build a aspx .net framework site in .net, then that computer will require the .net framework to have been installed, and require the correct type of software to work with those pages - in this case IIS.
Now, to be fair, you CAN do this with .net core, since .net core can run on quite much "any" computer (that supports nigex). However EVEN in that case, they FAKE and TRICK you, since how they achieve this "magic trick" is when you build the .net core application, the build process includes a WHOLE WEB SERVER as part of the build!!! (this lighter weight web server (kestrel) thus can work if you forward the ports and requests say from Apache, or quite much any web server. But MAKE NO MISTAKE here, a WHOLE .net core compatiable web server is included in that build, and thus that .net web server IS STILL required to run that .net code.
Unfortunately, the .net framework (as opposed to the .net core choice) does not upon build of your web site INCLUDE that WHOLE WEB SERVER as part of the build.
So, .net framework sites requite the correct software to work, and that correct software in this case is the web server called IIS (Internet Information Services).
You can no more suggest/attempt/assume that some program to load and read a PDF document is now out of the blue going to consume and work with Excel files, or word files.
hint:
Software build to a particular library of code requires the correct software to run and work. Kind of simple!!
So, a computer with a web server?
That is JUST a computer with software installed on it, and you need the correct software installed on that computer to consume the files etc. it was designed form.
Apache web server can no more consume a "application" or "web site" designed for IIS then it can consume other files etc. that are designed for use with a 100% different application. this is how all software works, and it not clear why you would think that Apache, or IIS, or word or Excel would work any different at all here?
Now, there was the "mono" project, and that was a port of .net framework that allowed the .net framework to run say on Linux, and there are some examples of some attempts to run aspx pages that way, but it is a galactic mess of epic proportions, and really not the way to go.
However, as stated, the .net core system DOES run on just about any platform, and it does have the above "magic trick" of being able to run on Apache, but behind the scenes, that software build actually includes a WHOLE WORKING web server that is .net core compatible, and that WHOLE web server is included with your build, and thus some are "fooled" by this magic trick that .net core web sites can run on Apache. They look like they can, but they are not, and in that example case (of .net core, NOT .net framework), the site seeming to work on the Apache web server? Nope, what occurs is the Apache web server simple hands off any web request to the REAL WORKING AND INSTALLED .net core web server!!! (thus Apache really only acts as a router, or "hand off" machine, and all the REAL web processing occurs in the .net core web server called kestrel).
So, you can do as you ask, but you would have to dump "web forms" and build the web site with aspx pages as a .net core web site. If you do that, then YES you can use Apache, and it will work, but behind the scenes, it ONLY works because that build process of the .net site ALSO includes THAT WHOLE COPY AND WORKING COPY of a .net core web server as part of the build. As noted, this magic trick option during the build process is NOT available for .net framework, but is most certainly available for the .net core choice.
So, in your case of using Apache? Software designed for a particular type of web server must use the correct type of web server. Same goes for IIS - it can't consume nor run a web site designed for Apache. I mean, how software works on your desktop is not any different then how software on a web server
The pages you have implemented with the .aspx are part of the ASP.NET Web Forms framework. This framework is part of the .NET Framework. The main server required to host Web Forms applications is IIS (Internet Information Services). Since you are working on a Windows machine, you can research how to setup IIS to host your Web Forms site if you wish. If you still want to use Apache as the front end for your site, you can even have it proxy requests to your site hosted in IIS, although there's probably little benefit and much complexity from such a setup.
Apache out of the box does not include support for hosting Web Forms applications. There is an open source project called Mono that seeks to implement an alternative version of .NET Framework and allows the use of alternative web servers such as Apache. But Mono has largely been superseded by .NET Core.
.NET Core is the official replacement for .NET Framework. It is open source and cross platform by default, and the ASP.NET Core web framework that runs on .NET Core includes a Kestrel web server. This can be utilized with Apache (or any other web server) in front of it, acting as a reverse proxy. It is not a magic trick, but a rather standard approach for web applications these days. However, ASP.NET Core does not include the Web Forms framework, so you would need to re-implement your application in a more modern alternative that's supported by ASP.NET Core if you intend to stay in the .NET ecosystem. There are many alternatives in ASP.NET Core including MVC, Blazor, Razor Pages, and Web API.

Setting up .NET Core development environments with IIS

I'm new to .NET core. We're currently implementing an Umbraco9 site which is running on .NET core 5 (MVC). Normally when setting up a development environment on a regular .NET site, I would create the project, point IIS to that same folder, and create a host entry for something like "local.mysite.com" I would then just be able to load up that URL in my browser and the site would serve, simply building when changes to back-end code were needed.
With .NET core, there are obviously some significant differences. For starters, there is a new wwwroot folder inside the project. So, in this case, I've tried pointing my IIS site to that wwwroot folder. This then has the impact of requiring a visual studio publish to that folder for any change front-end or back-end. Furthermore, it seems as though .NET core compiles the cshtml files into a Views.dll file, but when I publish the site, the site throws a 500 unless I manually copy the cshtml files to the wwwroot folder (which also has its own wwwroot after publish).
I've done loads of research on what the generally accepted best practices are with developing and hosting .NET core, but haven't come to a conclusion on this. Further complicating this is that I've seen some information saying that you can use Kestrel in conjunction with IIS. So now it's a matter of Kestrel vs. IISEx vs IIS. I'd prefer IIS as I'd like it to match production.
I've tried setting my IIS path to the path of the VS project, but I get "web server is not configured to list the contents of this directory." I enabled directory browsing, but this of course just lists the files in the directory. I've installed the hosting bundle as well.
The question: Is there a set of generally accepted practices on how to setup local development environments for .NET core projects? I'm really looking for some guidance on .NET core from a non .NET core developer's perspective. I'm trying to make this question as focused as possible, but I feel like there's a gap in my understanding of "old .NET" vs. .NET core and I'm hoping to get some guidance to help fill in that gap.
I wasn't able to get this figured out, so only solution I can come up with is to point IIS at a separate publish folder and publish to that folder when I need to make changes.

asp.net core app deploy as portable dll's (target full .net461)

In the context of asp.net core 1.0 apps there is the notion of portability. Either you pick to publish a portable app (dll's) and guarantee the installation of windows server hosting bundle on the server yourself and setup the site in IIS or you pick to publish a self contained app which doesn't need anything installed on the server and should not be setup in IIS.
Separately to this, there is a way of specifying the .net461 framework in project.json like so:
"frameworks": {
".net461": {}
},
When the project.json file contains that code, it automatically generates, what looks to be, a self contained app, which is an exe and a bunch of dependent dll's. Classic asp.net apps would produce purely dll's as you would host those in IIS.
My question is:
How do I make dotnet publish produce dll's and not exe's when specifying .net461 framework.
Another way to ask the question is:
How do I make dotnet publish produce a portable app when the frameworks node in project.json looks like this:
"frameworks": {
".net461": {}
},
so I figured this out... I assumed the standalone application meant that for asp.net core apps you wouldn't normally host them in IIS. That was an incorrect assumption. You still set them up exactly the same way in IIS regarldess of it being a portable application or standalone application. I had other issues which made me think that. I sorted out my other issues by turning on a debugging feature by using this UseSetting method in Program.cs:
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseSetting("detailedErrors", "true")//TODO: make this config driven to not be deployed in prod. needed for alpha debugging.
.Build();
host.Run();
that was equivilant to classic asp.net's "customErrors=off".

How to host an ASP.NET MVC project in IIS?

I have an ASP.NET MVC solution with multiple project under it, it works fine locally after uploading the site on the server I get the System.Reflection.ReflectionTypeLoadException error
I am also trying to run the solution on IIS and the error I am getting is Not Found
The requested URL / was not found on this server.
I have also copied all the DLL to the output folder, what could be the issue is the problem most likely from a missing DLL, is it from the remote hosting security level or am I missing something else in the project please advise.
After calling the hostgator (where the site is hosted) and addressing the issue, I was told that custom DLL is not allowed in our hosting as you may see I have taken screenshots of the DLLs I wonder what are the customs DLL in my project as hostgator technician put it
I assume you're running v3 of ASP.NET MVC. In which case, have you set the Application Pool .NET Framework version in IIS to C# 4.0? Indeed, is ASP.NET MVC and .NET Framework 4.0 installed on your server?
Does your application rely on any external libraries which were not included in the deployed application?
Are you able to debug your application at all? Ie the Application_Start. It's possible that something is going awry during that which is causing this.
Basically, more information would help!

How to run a leightweight ASP.NET MVC application that would be accessible only locally (not on IIS)?

We have a desktop client application and recent customer requests indicate that they would like to have some dynamic HTML content served and displayed by the application.
We are considering hosting a simple ASP.NET application in a local process, accessible only from the local machine (similar to the ASP.NET development web server used when debugging from Visual Studio).
How can we run an ASP.NET application locally without IIS? IIS is not an option because most client machines will not have it installed.
Is there a leightweight ASP.NET web server that could run locally similar to the development web server that VS is using?
I have found some information about "Cassini". Is this suitable for production use? Can it handle ASP.NET MVC? Are there any alternatives we should consider?
I have not used it myself, but you can try the mono XPS server.
It is a stand alone webserver.
The easiest way to start XSP is to run it from within the root directory of your application. It will serve requests on port 8080. Place additional assemblies in the bin directory.
Cassini is in fact also a good option - it is the development web server that comes with visual studio (so widely distributed, used and tested) and is also used by the open source ScrewTurnWiki. See wikipedia.
In regards to your "only locally" requirement - a web server will serve any request made to the right port. In order to limit accessibility, you should use a firewall that will block any external requests.
You might consider using WCF to host a service on the local machine that can serve the data without having to host a full blown web server.
If you do this, WCF allows you to expose the service with multiple endpoints and make it available through HTTP, TCP, or Namepipes. Namepipes would restrict traffic to only the local machine.
I have also tried IIS Express. It works great with ASP.NET MVC. Right now it is available only with Web Matrix, but installing web matrix is easy.
Coming back to this question three years later, ServiceStack.NET with self-hosted option seems like a good choice. While it is not ASP.NET MVC directly, it provides a good API and features are on par with ASP.NET MVC/WebAPI (or in some ways better).

Resources