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.
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
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2018-01-18 Jenkins project
2018-01-18 sql server drop login failed
2018-01-18 devenv.exe 编译Solution