How does IIS Know how to start .NET Web Application - c#

I'm trying to understand how IIS knows how to start my ASP.Net Web Application My understanding so far is that, when creating a web application we create a Web.Config which defines how IIS will start it's process
So We have a Web Config
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="LogRequests" type="BDBPayroll.Apps.API.Web.Shared.HttpModules.LogRequestsHttpModule, BDBPayroll.Apps.API.Web.Shared" />
<add name="MiniProfiler" type="BDBPayroll.Apps.API.Web.Shared.HttpModules.MiniProfilerHttpModule, BDBPayroll.Apps.API.Web.Shared" />
</modules>
<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>
And The Global Asax:
public class WebApiApplication : WebApiHttpApplication<WebModule>
{
protected override void Configure(HttpConfiguration config)
{
config.Filters.Add(new Filters.ContextResolverFilter());
config.Filters.Add(new ValidateModelAttribute());
config.Filters.Add(new PaginationFilter());
GlobalContext<JsonFormatterRule>.Instance.SetDefaultJsonFormatter(config);
}
//...
}
Since IIS can run multiple applications e.g php, .net etc, How does IIS Know from the Web Config To run the Global Asax.
My guess is that it looks up the application type from the web config, and then searches for WebApiHttpApplication, Does anybody have any more information on this process?

Since IIS can run multiple applications e.g php, .net etc, How does IIS Know from the Web Config To run the Global Asax.
My guess is that it looks up the application type from the web config, and then searches for WebApiHttpApplication, Does anybody have any more information on this process?
As far as I know, if use send the request to the IIS.
After handling the http request by http.sys, IIS will move this request to the w3wp.exe to handle it.
Since IIS could only handle htm or html static page, IIS will use ISAPI to handle the page which IIS couldn't handle.
ISAPI is a kind of extention handler to handle different kinds of pages like php, aspx, cshtml or something else.
You could find it from IIS manager console handler mapping icon.
Image as below:
The IIS will send the request to right http hanlder according to its extension. The handler moudule(e.g asp.net isapi) will load the CLR and web application(include the globalasax) to handle the request.

Related

.NET Webforms routing works on localhost but not on IIS 8

I am creating a .NET web forms application and attempting to use friendly URLs for the first time. The application is targeting framework 4.7.1. Everything is easy and works great on localhost. When I deploy it to a testing server (which is running IIS version 8.5.9600.16384), I get some odd results. The page will route correctly, and retain the friendly url structure, if the routeUrl is equal to the physical page name, but if the routeUrl is not equal, it gives a 404. Even when the route does work, the route values don't come through to the page. Page.RouteData.Values.Count always comes back as 0, even when I'm setting a default value.
I have looked all over Stack Overflow and the rest of the internet and looked at I don't even know how many posts from people with similar problems, but in the few cases that there were suggested solutions, none of them worked for me. See below things I have changed in my web config based on those suggestions, all to no avail.
What I have right now:
RouteConfig.cs
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute( //"works", but no route values when navigate to /giftcard/something
"new-giftcard",
"giftcard/{action}",
"~/giftcard.aspx",false, new RouteValueDictionary { { "action", "add" } });
routes.MapPageRoute( //returns 404 when navigate to /random/something
"randomtest",
"random/{action}",
"~/giftcard.aspx", false, new RouteValueDictionary { { "action", "test" } });
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
}
}
Global.asax
void Application_Start(object sender, EventArgs e)
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
Web.config (things I added based on research of possible issues, none of which helped)
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" >
<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
</modules>
<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>
All of this seems to work perfectly fine on localhost. Because it all works locally, there has to be something wrong with how IIS is handling the routing, but I am out of my area of expertise here, so any other ideas for what to change or how to debug would be great.
Here's my current settings on the live server again for reference:
IIS version 8
Application pool .NET 4.0 Integrated
Target framework .NET 4.7.1
I checked IIS Express on my local development machine, and it's version 10.
EDIT: further info - I tried it on a new test server with a fresh install of server 2012, and it seems to work. Unlike the test server described above, this new server has ONLY had .NET 4.0 installed. The first server is older and had .NET 3.5 and 4 both installed. Perhaps a conflict with versions of .NET?

Web API On IIS 10 (Handler "ExtensionlessUrlHandler-Integrated-4.0" has a bad module "ManagedPipelineHandler")

I built a basic RESTful web service so that I can receive events. I can run this fine on local IIS, but have the following error when publishing it in IIS 10 on a server
HTTP Error 500.21 - Internal Server Error
Handler "ExtensionlessUrlHandler-Integrated-4.0" has a bad module "ManagedPipelineHandler"
I set the application pool Managed Pipeline Mode to Integrated and enabled Directory Browsing in the app features. My web config looks like this:
<system.webServer>
<modules>
<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
</modules>
<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>
If I change the application pool Managed Pipeline Mode to "Classic" I can actually see the root site but trying to browse or call to the API gives 404s (such as xx.x.xxx.x/api/Controller/Action).
Using IIS 10 and .NET 4.6
Add roles and Features
Open up child nodes for WEB Server IIS
Under Web Server, open nodes for Application Development
Select ".NET Extensibility 4.6", "ASP.NET 4.6"
These resolves the problem.
In Windows 10 I was able to resolve this via:
Control Panel > Programs & Features > Turn Windows features on or off
Removing .NET Framework 4.7 Advanced Services > ASP.NET 4.7
Adding it back
for anyone else that runs into this I installed ASP.net 4.6 on Server 2016 in add remove roles and this resolved my issue.

Simulating WCF route in WEB API 2

We currently have an old legacy WCF service that I really don't want to maintain but several clients still have access. We currently run all our API services in WEB API 2 in Azure. To enable backward compatibility I was hoping to simulate the WCF routing in WEB API using attribute routing like so:
[RoutePrefix("registration.svc")]
public class RegistrationDeprecatedController : ApiController
{
[HttpPost]
[Route("login")]
public async Task<HttpResponseMessage> Login(CredentialModel creds)
{...
Locally this runs like a dream. As soon as I publish to an Azure AppService however all I get is 404 not found. The specific route in question:
/registration.svc/login
My suspicion is that IIS hosting has the .svc route file associate to process the requests as a WCF call. An extensive search online failed to find anything on how I can change this association or even confirm if this is the case. Any ideas on how to fix or alternative solutions will be greatly appreciated.
Update
Found a solution but its not perfect. Found out that it is possible to directly connect to IIS on the AppService instance and change the Handler Mappings. This is not perfect as it would require a manual change when creating a new instance or deployment slot and I would prefer to automate the entire process.
If anyone else would like to do this you can follow a tutorial by benjamin perkins. I did have to install an IIS Manager for remote administration extension on windows 10 however for this to work. I removed all mappings associated to *.svc and the routing is now working.
Final solution was to remove the handlers in the web.config.
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<remove name="svc-Integrated" />
<remove name="svc-Integrated-4.0" />
<remove name="svc-ISAPI-2.0" />
<remove name="svc-ISAPI-2.0-64" />
<remove name="svc-ISAPI-4.0_32bit" />
<remove name="svc-ISAPI-4.0_64bit" />
....
</handlers>
I was able to find the names by connection to IIS on the AppService instance. I was unsure which ones needed to be removed so I removed all that handlers associated with *.svc.

Make IIS 7.5 pass *.xml requests to asp.net

How do I configure IIS 7.5 to forward all *.xml file requests to asp.net engines so i can handle them in Global.asax and rewrite the path to a *.aspx file? Now IIS is expecting to find them directly on disk. I will use this do dynamically generate my sitemap.xml
You can force static files to go through the ASP.NET pipeline by editing your web.config:
<system.webServer>
<handlers>
<add name="XMLHandler" type="System.Web.StaticFileHandler" path="*.xml" verb="GET" />
</handlers>
</system.webServer>
HTTP Handlers and HTTP Modules Overview
How to: Register HTTP Handlers

ASP.NET MVC Routing - add .html extension to routes

i am pretty new to MVC and Routing and i was asked to modify an app to use diffrent url's.
a task that is a bit over me since i have no experience.
ok, lets talk a bit of code:
routes.MapRoute(
"CategoryBySeName", // Route name
"products/{SeName}", // URL with parameters
new { controller = "Catalog", action = "CategoryBySeName" }
);
this works as expected, but then the client wanted ".html" at the end of paths, so i changed:
"products/{SeName}", // URL with parameters
to:
"products/{SeName}.html", // URL with parameters
which fails ( IIS 404 page - MapRequestHandler)
it seems like iis is trying to load a physical file with that name instead of passing it to the application.
Similar: ASP.NET MVC Routing to start at html page (not answered, Not duplicate)
You have to force all request through the ASP.NET pipeline, and you can do that by adding only this single line to the web.config of your application:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
You're guess that an IIS handler is probably grabbing the request prior to MVC is likely correct.
Assuming IIS 7:
http://technet.microsoft.com/en-us/library/cc770990(v=ws.10).aspx
You need to edit the .html handler in IIS to use ASP.NET.
You can find it in the website properties under the home directory tab in app configuration in the mappings section in II6.
Something along the lines of (version may be different):
C:\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll is what you need to handle the .html files.
Changing the Application Pool from Classic to Integrated fixed the issue.
thank you guyz for your help.
Just add this section to Web.config, and all requests to the route/{*pathInfo} will be handled by the specified handler, even when there are dots in pathInfo. (taken from ServiceStack MVC Host Web.config example and this answer https://stackoverflow.com/a/12151501/801189)
<location path="route">
<system.web>
<httpHandlers>
<add path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" />
</httpHandlers>
</system.web>
<!-- Required for IIS 7.0 -->
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="ApiURIs-ISAPI-Integrated-4.0" path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
</location>

Categories