c# - "MapHttpRoute"和 "MapRoute"之间的区别?

标签 c# asp.net asp.net-mvc asp.net-web-api asp.net-mvc-routing

为什么使用“MapRoute”作为“Default”路由,而使用“MapHttpRoute”作为“DefaultApi”路由?

routes.MapHttpRoute(
  name: "DefaultApi",
  routeTemplate: "api/{controller}/{action}"
);

routes.MapRoute(
  name: "Default",
  url: "{controller}/{action}/{id}",
  defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

最佳答案

如果您在 ASP.NET 之上使用 Web API,它们最终都会在同一个底层 ASP.NET 路由表上运行 - 然而,正如正确指出的那样,从用户的角度来看,您调用两种不同的方法来注册路由。

路由是这样设计的,以便在 ASP.NET 外部托管时,Web API 不必依赖于 System.Web。

请记住,Web API 并不位于 MVC、Web 窗体或 ASP.NET 之上。它可以托管在 Web 上下文 (ASP.NET) 中,但也可以自行托管(控制台、WPF 等),甚至托管在内存中(无需使用端口,对于轻量级端到端测试很有用)。

关于c# - "MapHttpRoute"和 "MapRoute"之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12042853/

相关文章:

c# - 解码 Opera 缓存内容

c# - 有什么方法可以保护 asp.net MVC4 web 应用程序免受来自外部网站的请求?

c# - 将用户脚本引擎添加到我的应用程序

asp.net-mvc - asp.net mvc : thousand separator for int value

c# - Linq 中嵌套选择的替代方法

c# - 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 失败

c# - wcf 服务中的 SqlDependency 缓存不驱逐

java - 简单的 android 和 ASP.NET 组合

c# - 我可以从 ASP.NET 页面下载由内存流生成的 Excel 文件吗?

c# - 在 ASP.NET MVC 中使用 POST 传递变量