How to get IIS to recognize OWIN startup class? - c#

My OWIN web service runs beautifully in Visual Studio 2013, but when I publish it to a real IIS site, it acts as if the Configuration method in the startup class has not been run. I can do "normal" things like browse to the app and see the directory structure, but nothing that was supposedly set up with the IAppBuilder is functional. For example, I get a 404.0 error when I browse to a URL that was set up in Startup to issue an OAuth2 bearer token. It's as if Startup.Configuration(IAppBuilder app) was never run.
I'm using the [assembly: OwinStartup(typeof(MyNamespacedStartupClass))] attribute to designate the startup class.
I've used NuGet to get both Microsoft.Owin.Host.SystemWeb and Microsoft.Owin.Diagnostics per instructions I've seen, but that doesn't make a difference.
What more do I have to do?

Make sure your app pool is in v4.0 integrated mode.
Make sure you have bin placed Microsoft.Owin.Host.SystemWeb (I see you have installed it) - Just make sure its also in the bin folder.
This article will have more information on how an OWIN middleware runs on Integrated pipeline.

I also had to add an extra setting to my web.config
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
From: https://katanaproject.codeplex.com/wikipage?title=Static%20Files%20on%20IIS
IIS has a native static file module that is optimize to skip other
portions of the pipeline if it sees file paths that do not match other
handlers (e.g. not aspx). This means that the directory browser
middleware is likely to work, but then the static file middleware may
be bypassed in favor of the native static file module.
This tells IIS not to skip the managed Asp.Net modules even if the
native static file module thinks it has a match.
It also describes another step, but this was not needed for me:
Also, add the following stage marker AFTER your static file middleware
(in namespace Microsoft.Owin.Extensions):
app.UseStageMarker(PipelineStage.MapHandler);

Probably the reason if you upgraded at some point from an older MVC:
Make sure you don't have
<add key="owin:AutomaticAppStartup" value="false" />
in your web.config. It will suppress calling the startup
Instead change it to this
<add key="owin:AutomaticAppStartup" value="true" />
Somewhere along the line - when I upgraded to MVC 5 this got added (actually almost ironically it was a year ago tomorrow) and I never even knew what owin was until today when I tried to use it.

I also faced same problems when I migrated my already running MVC5 site to a new server. It gave me nightmares, just to recap I had to do all this to get it working
Add [assembly: OwinStartupAttribute(typeof([YourAssemblyName].Startup))] this to the Startup class (after the using statements and before the namespace declaration)
Add these keys to the <appSettings> section of web.config
<add key="owin:AppStartup" value="[NamespaceForYourStartUpClass].Startup, [YourAssemblyName]" />
<add key="owin:AutomaticAppStartup" value="true" />
And lastly as suggested by Martijn Evens add the following to <system.webserver> section in web.config
<modules runAllManagedModulesForAllRequests="true" />

For those who deal with legacy and (or) have migrated versions. Check windows "Roles and features", find what version of ASP.net is installed, and use exactly the same version in web.config for targetFramework, for example in my case it was 4.6 not 4.8, so
<system.web>
<httpRuntime targetFramework="4.6" requestValidationMode="2.0" maxQueryStringLength="2097151" />
<compilation targetFramework="4.6" optimizeCompilations="true">

Related

getting swagger to run in asp.net

I've inherited a class library that is using .NET Framework 4.7.2. In the library there are a bunch of ApiControllers with a number of HttpPost and HttpGet Methods on them. Errors have started occurring on some of the methods and these methods are typically called from a wordpress site.
To try and make things a bit easier for me to debug, I'm trying to install swagger into the solution. I've installed Swashbuckle.core with a runtimne version of 4.0.30319 and this has created a basic SwaggerConfig.cs file in my App_Start folder.
In the properties for the solution the project url is http://localhost/WordpressService and it's running under local IIS.
I have a local version of wordpress running in Microsoft Edge, and in IIS I can see that the WordpressService is running on port 80.
So now in the WordpressService solution I try to attach the debugger to the browser, then in the browser I try to enter http://localhost/WordpressService/swagger and other variations of the address, but all the time I receive the following error:
HTTP Error 500.21 - Internal Server Error Handler
"ExtensionlessUrlHandler-Integrated-4.0" has a bad module
"ManagedPipelineHandler" in its module list
I have this section in my web.config file:
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer> >
within IIS in the Handler Mappings section I can see ExtensionlessUrlHandler-Integrated-4.0 and it is enabled.
it also suggests that asp.net may not be installed correctly but it must be as I'm using VS 2017 and would need to be there for that. Looking around the net, the default SwaggerConfig.cs should be enough to get me going.
The only thing I can think of is that I may have installed the wrong version of swagger, but I'm not sure in that case which version I should be installing for this .NET Framework.
Ok, I figured the issue out and thought I'd post my answer for anyone else that may come across this issue. From IIS installation, it appears that only Framework 3.5 had been installed and not Framework 4.8. Installing 4.8 in the IIS section seems to have resolved this and I can now see my end points via swagger.

Hooking up sub applications in ASP.NET Core

I have two projects in asp.net core 2.1. One is the main web application, and the other is a sub-application. The sub-application is for the most part, independent; it only needs cookies from the main application.
I'm trying and failing to set up the two projects in my local dev environment, and I will also need to set up for live IIS Deployment in the future. I have both VS projects in the same VS Solution. In their project Debug settings page, I have them both set up to launch IISExpress with app URL as 'http://localhost:44305'. I have tried setting the subapp to 'http://localhost:44305/subapp' too, but when vs run, IISExpress complains that the port is already in use.
I have a web config for both apps. The very basic defaults for both. In the sub app, I have the extra line to remove the aspnetcore handler (as I found from searching).
<remove name="aspNetCore" />
In the sub app, i have also tried adding in startup.cs:
app.UsePathBase("/subapp")
The results I get from this setup running in vs locally is that on the base URL, it actually loads up my subapp instead of my main app (browser URL is localhost:44305). Fiddling with multiple settings seems to get me nowhere; either both apps fail to load completely, or one or the other app is inaccessible.
Is there any proper documentation out there for setting up multiple apps to work together locally and in IIS? Everything I seem to find is from pre-2.0.
Thank you for your help!
Edit:
I actually managed to get a fresh project working as subapp when deploying to IIS. To set up:
Make a new VS solution with two aspnetcore 2.1 web applications.
Add a web.config to sub application with following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
</handlers>
<aspNetCore processPath="dotnet" arguments=".\IISTestSubApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
</system.webServer>
</configuration>
No web config for Main app.
Add .UseIISIntegration() to Program.cs CreateWebHostBuilder to both apps.
In Startup.cs Configure method for Subapp, add app.UsePathBase("/subapp/");
Deploy main app to a folder /Mainapp. Deploy sub app to /Mainapp/subapp.
Set up iis website for main app.
Right click on main app website in IIS and click Add Application. Add the subapplication with an alias '/Subapp'
That should be all you need. Some of the steps may not be necessary. I haven't fully tested which steps are 100% necessary.
Unfortunately, I still can't get it to work with IISExpress. I have my .vs/applicationhost.config file looks almost exactly like the IIS one, but when running both apps at the same time, i get errors saying that the port is already in use. I'm still trying a bunch of new things, so, we'll see if I can figure it out.
I got it working now in IISExpress. Following the above steps for IIS first, for basic setup, include the following:
The subapp web.config file should look like this for IISExpress:
<configuration>
<system.webServer>
<handlers>
</handlers>
<aspNetCore processPath="dotnet" arguments=".\bin\Debug\netcoreapp2.1\IISTestSubApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
</system.webServer>
</configuration>
In your solution's .vs/config/applicationhost.config file, make sure your sites section looks like
<site name="IISTest" id="2" serverAutoStart="true">
<application path="/" applicationPool="IISTest AppPool">
<virtualDirectory path="/" physicalPath="C:\Users\user\Documents\GitHub\IISTest\IISTest" />
</application>
<application path="/subapp" applicationPool="IISTest AppPool">
<virtualDirectory path="/" physicalPath="C:\Users\user\Documents\GitHub\IISTest\IISTestSubApp" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:44301:localhost" />
</bindings>
</site>
It seems okay that both apps use a different application pool. Setting them the same gets overwritten when vs runs, for some reason.
Run only the main app from vs; Don't try to run sub apps as well. You will get port already taken error message.

C# and Razor - The type of page you have requested is not served because it has been explicitly forbidden. The extension '.cshtml' may be incorrect

I have looked through other posts but none seem to answer what I need.
I created an empty site in WebMatrix (ASP.NET)
I opened that site in VWD 2013
I hit F5 and it runs fine on a URL such as http://local.com:59833/ContentPage.cshtml
I go to http://local.com/cscsu_bi/ContentPage.cshtml and it doesn't work with the error below
Server Error in '/' Application.
This type of page is not served.
Description: The type of page you have requested is not served because it has been explicitly forbidden. The extension '.cshtml' may be incorrect. Please review the URL below and make sure that it is spelled correctly.
Requested URL: /cscsu_bi/ContentPage.cshtml
The web.config file is as follows
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="webpages:Enabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
I am on Windows 7. Is there anything obvious I'm doing wrong?
Thanks
I was missing the Microsoft.AspNet.WebPages NuGet package. Installing it solved my problem. (This package has dependencies on Microsoft.Web.Infrastructure and Microsoft.Web.Razor and thus includes them for you)
If you use the NuGet console, just type:
Install-Package Microsoft.AspNet.WebPages
I have set up Razor on a number of machines, and often find that I need to include the following DLLs in my bin folder:
System.Web.Razor.dll
System.Web.WebPages.Deployment.dll
System.Web.WebPages.dll
System.Web.WebPages.Razor.dll
Microsoft.Web.Infrastructure.dll
Take a look at this article.
http://www.asp.net/web-pages/overview/more-resources/aspnet-web-pages-(razor)-troubleshooting-guide
This fixed my issue after reviewing all IIS settings and references.
Make sure that the root of your website has at least one .cshtml file in it.
This may not be your exact issue, but I have had this happen for a couple reasons:
Incorrect Framework on Server: Make sure the target framework of the
project is supported by the server. Just changing the
targetFramework attribute of the compilation element may not be
enough as the DLLs for your project may be compiled
against the 4.5 framework.
Incorrect Framework of Application Pool: This can also happen if
your application pool is not using the right .Net CLR version. Make
sure it is using the 4.0 instead of 2.0
In my case the culprit was the fact that Global.asax was placed under the '/Views' folder. This was somehow working while the project was targeting MVC3. Once the solution got upgraded to MVC4, running said solution would result in the aforementioned errors. In a similar vein, other errors would crop up mentioning:
"no default webpage has been set and directory browsing has been disabled"
The solution: After upgrading to MVC4+ I had to manually move Global.asax under the root folder of the container project. Be sure to inspect Global.asax to make sure that the namespace applied within is the proper one. Just my 2c.
In my case I set webpages:Enabled to true under appSettings:
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
According to post: what is the function of webpages:Enabled in MVC 3 web.config, this prevents files in the Views folder from being directly accessible from a web browser.

Forbidden to browse WCF svc file?

I am using VS2010 + C# + .Net 4.0 + IIS 7.5 + Windows 7. When I open an svc file (in IIS manager, right click the svc file and select browse) for a WCF project in IIS, there is an error like this, any ideas what is wrong?
This type of page is not served.
Description: The type of page you have requested is not served because it has been explicitly forbidden. The extension '.svc' may be incorrect. Please review the URL below and make sure that it is spelled correctly.
Here is the content of the web.config file I am using, is it correct?
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<remove fileExtension=".svc" />
</staticContent>
<handlers>
<remove name="svc-ISAPI-2.0" />
<remove name="svc-Integrated" />
<add name="svc-ISAPI-2.0" path="*.svc" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv2.0,bitness32" />
</handlers>
</system.webServer>
</configuration>
You may have to launch the command servicemodelreg –ia in Visual Studio command prompt.
If it's not working, check if you have "WCF http activation" feature installed (Add/Remove Programs - Turn Windows Features On or Off - Microsoft .Net 3.5.1).
UPDATE 1: Probably your application is hosted under .NET 2.0 App Pool. Go to IIS Manager and check for Basic Settings… link. Click Select button and you will see which option are you using. Change it to use .NET 4.0
UPDATE 2: Your web.config file seems incomplete. I suggest you to configure your service using WCF Configuration Editor tool. Launch it (make sure you are using .NET 4 version of the tool) and open your web.config file. Create service configuration and service endpoint configuration, then save it.
Its likely the mime types not setup correctly on IIS, try using the aspnet_regiis tool. Failing that, I'd recommend adding a mimetype to your virtual directory in iis for .svc files to be handled by the .NET runtime.

Getting "The WebResource.axd handler must be registered in the configuration to process this request." error

I'm getting this error while running my ASP.NET app on IIS7. I've tried doing what it says to do but it doesn't help.
The WebResource.axd handler must be
registered in the configuration to
process this request.
> <!-- Web.Config Configuration File -->
>
> <configuration>
> <system.web>
> <httpHandlers>
> <add path="WebResource.axd" verb="GET" type="System.Web.Handlers.AssemblyResourceLoader" validate="True" />
> </httpHandlers>
> </system.web>
> </configuration>
I'm using a little bit of AJAX which is what I think is causing the issue. Has anyone encountered this before?
I figured it out so I'm posting it here for search reasons. It is a bug in ASP.NET and it has to do with having ColdFusion installed. Microsoft hasn't yet released a fix.
There are two ways to fix this.
Change the AppPool in IIS7 to "Classic .NET AppPool". I'm not sure of any implications with this setting.
Remove all the references to ColdFusion from your applicationHost.config file in system32\inetsrv\config.
ColdFusion installs a global wildcard handler mapping which apparently overrides many of the standard .NET handlers. The solutions mentioned work just fine, but if you can't switch to Classic Mode and don't want to screw with your ColdFusion installation, you can remove the inherited handler mapping at the individual site level.
To do this, go to the site in question in IIS, double-click on "Handler Mappings" under the "IIS" section, and find the handler named something like "AboMapperCustom-XXXXXX" with "*" for the Path. Select the entry and click "Remove" in the sidebar. This will only remove the mapping for your application, so it won't break any existing CF sites on the server.
In IIS7 you need to add the <httpHandler> section to <system.webServer> instead of <system.web>. Here is an example.
I got this error after carelessly copying my app's web.config between a pair of clustered servers, which overwrote the tag:
<system.webServer>
<handlers>
<remove name="AboMapperCustom-XXXXXXXX" />
</handlers>
</system.webServer>
with
<system.webServer>
<handlers>
<remove name="AboMapperCustom-YYYYYYYY" />
</handlers>
</system.webServer>
Locating the proper ID as per Josh's response and correcting the tag fixed it, but more importantly, will presumably keep that handler mapping from sneaking back in.
The issue happened to me on new Windows 2016 server where ASP.NET 4.6 was not installed. After installation everything got fixed.
Steps
- Run Server Manager
- Manage > Add Roles and Features
- Server Roles
- Web Server (IIS) > Web Server > Application Development > ASP.NET 4.6
I had this problem and that reason was incompatibility between Coldfusion and some configurations of ASP.NET applications when IIS App pool is in integrated mode. Coldfusion must be disable .

Categories