之间&QUOT差异; MapHttpRoute"和"图路线"? [英] Difference between "MapHttpRoute" and "MapRoute"?

查看:94
本文介绍了之间&QUOT差异; MapHttpRoute"和"图路线"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要使用图路线为默认路由,同时采用MapHttpRoute为DefaultApi路由?

  routes.MapHttpRoute(
            名称:DefaultApi
            routeTemplate:API / {控制器} / {行动}
        );        routes.MapRoute(
            名称:默认,
            网址:{控制器} / {行动} / {ID}
            默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
        );


解决方案

如果您在ASP.NET的顶部使用的Web API,他们将最终都在同一个基础ASP.NET路由表进行操作 - 然而,由于正确地指出,从用户的角度调用两种不同的方法来注册的路线。

路由的目的是这样的,这样的托管ASP.NET外时,网络API将不必依靠的System.Web。

请记住,网络API不是坐在MVC之上,Web窗体,或者,对于这个问题ASP.NET的。它可以网页上下文(ASP.NET)内举行,但也可以自托管(控制台,WPF等),甚至在内存托管(不使用的端口,有用即轻量级终端到终端的测试)。

Why using "MapRoute" for "Default" routing, while using "MapHttpRoute" for "DefaultApi" routing?

        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 }
        );

解决方案

If you use Web API on top of ASP.NET they would ultimately both operate on the same underlying ASP.NET route table - however as correctly pointed out, from the user perspective you call two different methods to register route.

Routing was designed like this so that when hosting outside of ASP.NET, Web API wouldn't have to rely on System.Web.

Bear in mind that Web API is not sitting on top of MVC, Web Forms, or, for that matter ASP.NET at all. It can be hosted within web context (ASP.NET) but can also be self hosted (Console, WPF etc) or even hosted in memory (without port use, useful for i.e. lightweight end-to-end testing).

这篇关于之间&QUOT差异; MapHttpRoute"和"图路线"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆