ASP MVC消息处理VS网页API消息处理程序 [英] ASP MVC Message Handlers vs Web API Message Handlers

查看:150
本文介绍了ASP MVC消息处理VS网页API消息处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了2个项目:


  1. 普通,基本的ASP.NET MVC应用程序4

  2. 基本ASP.NET的WebAPI应用

我所做的是我将自己的自定义消息处理程序,从 DelegatingHandler 导出到他们两个。在这里,它是:

What I did is I added my custom message handler, derived from DelegatingHandler to both of them. Here it is:

public class MyHandler : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
    {
        return base.SendAsync(request, cancellationToken);
    }
}

我的注册它的Global.asax

GlobalConfiguration.Configuration.MessageHandlers.Add(new MyHandler());

我把一个断点

return base.SendAsync(request, cancellationToken);

ASP.NET MVC和ASP.NET的WebAPI之间的探源是,当我打电话MVC应用程序(的http://本地主机:4189 /东西的断点不会触发。当我调用Web API但(的http://本地主机:7120 / API /值),断点被触发

The diffrence between ASP.NET MVC and ASP.NET WebAPI is that when I call MVC application (http://localhost:4189/Something) the breakpoint is not triggered. When I call Web API however (http://localhost:7120/api/values), the breakpoint is triggered.

这是为什么?有没有在这些类型的应用程序执行流?

Why is that? Are there any differences in those application types execution flows?

此外,当我尝试请求正常控制器,而不是 ApiController 的WebAPI应用,像的http://本地主机:7120 /首页,破发点的则不会触发

In addition, when I try to request normal Controller, not ApiController of WebAPI application, like http://localhost:7120/Home, the break point is not triggered.

推荐答案

您可以比较这两个MVC和的WebAPI

You can compare the route configuration methods of both MVC and WebAPI

对于MVC

可用的过载是,

  public static class RouteCollectionExtensions
    {

        public static VirtualPathData GetVirtualPathForArea(this RouteCollection routes, RequestContext requestContext, RouteValueDictionary values);

        public static VirtualPathData GetVirtualPathForArea(this RouteCollection routes, RequestContext requestContext, string name, RouteValueDictionary values);

        public static void IgnoreRoute(this RouteCollection routes, string url);

        public static void IgnoreRoute(this RouteCollection routes, string url, object constraints);

        public static Route MapRoute(this RouteCollection routes, string name, string url);

        public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults);

        public static Route MapRoute(this RouteCollection routes, string name, string url, string[] namespaces);

        public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints);
       namespaces.

        public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, string[] namespaces);

        public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints, string[] namespaces);
    }

有关的WebAPI

    public static class HttpRouteCollectionExtensions
    {
        public static IHttpRoute MapHttpRoute(this HttpRouteCollection routes, string name, string routeTemplate);

        public static IHttpRoute MapHttpRoute(this HttpRouteCollection routes, string name, string routeTemplate, object defaults);

        public static IHttpRoute MapHttpRoute(this HttpRouteCollection routes, string name, string routeTemplate, object defaults, object constraints);

        public static IHttpRoute MapHttpRoute(this HttpRouteCollection routes, string name, string routeTemplate, object defaults, object constraints, HttpMessageHandler handler);

    }

见,最后的WebAPI路由配置方法,你可以通过定制HttpMessageHandler你想要的参数。 MVC路由没有在其管道的规定。

See, the last webapi route configuration method has a parameter where you can pass customized HttpMessageHandler you want. MVC routing does not have that provision in its pipeline.

在总结,MVC的执行上下文和管道距离的WebAPI完全不同的,由于你的破发点不棒,你要了这个事实。

In summation, the MVC execution context and pipeline are totally different from WebAPI due to that fact your break point does not sticks where you want.

希望有所帮助。

这篇关于ASP MVC消息处理VS网页API消息处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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