Difference between RouteTable.Routes and HttpConfiguration.Routes?

https://stackoverflow.com/questions/12533782/difference-between-routetable-routes-and-httpconfiguration-routes

The HttpConfiguration class is specific to WebApi making it reusable outside of IIS (e.g. For self hosting). The RouteTable version is for IIS only.

 

https://stackoverflow.com/questions/23381962/routetable-routes-vs-httpconfiguration-routes

 

RouteTable is the underlying structure for routing used by asp.net/System.Web.

When you run on IIS using ASP.NET (which is the default template in Visual Studio) the routes are written to the routetable, and WebHost is used to shim between ASP.Net/System.Web and Web API. In that case the routes are written to the RouteTable.

But Web API can run in WCF self host or Owin self host as well (in a console app, a service or hosted in your application code). In these cases ASP.NET (or more accurately) system.web is not being used as the hosting layer. These two hosts do not provide an abstraction for Routing and Web API implements its own routing.

To keep the user code portable between different hosts, the routes are written to a separate collection, and then each host makes a different use of it.

As a side note, this is also the reason using HttpContext.Current is not recommended in Web API if you ever plan to run Web API in a none IIS host.

 

https://forums.asp.net/t/1845358.aspx?Difference+between+HttpConfiguration+Routes+and+RouteTable+Routes+

I am wondering why routes seem to be configurable from both the HttpConfiguration object and the RouteTable class? Clearly the two .Routes properties are not of the same type, but they seem to stay in-sync. Is there a chance they could get out-of-sync? I'm wondering though, from a Web API perspective, should RouteTable.Routes ever be used?

 

WebAPI can be hosted outside of IIS so they needed an abstraction on the RouteTable.Routes.

 

That makes sense. After I did some more digging, I see that the HttpConfiguration.Routes collection really just wraps the RouteTable.Routes collection (when hosted on ASP.NET) so I suppose there's really no big difference between the two.

 

2个属性的类型是不同的:

RouteTable.Routes 的类型是System.Web.Routing.RouteCollection  Assemblies:System.Web.dll, System.Web.Routing.dll

HttpConfiguration.Routes 的类型是 System.Web.Http.HttpRouteCollection  Assembly:  System.Web.Http (in System.Web.Http.dll)

 

下面的扩展方法,只对RouteTable.Routes做了

https://docs.microsoft.com/en-us/previous-versions/aspnet/dn253837(v%3Dvs.113)

#region Assembly Microsoft.Owin.Host.SystemWeb, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// packages\Microsoft.Owin.Host.SystemWeb.4.0.0\lib\net451\Microsoft.Owin.Host.SystemWeb.dll
#endregion

using Owin;

namespace System.Web.Routing
{
//
// Summary:
// Provides extension methods for registering OWIN applications as System.Web routes.
public static class RouteCollectionExtensions

 

 

posted @ 2019-01-18 14:11  ChuckLu  阅读(364)  评论(0编辑  收藏  举报