ASP MVC消息处理VS网页API消息处理程序

编程入门 行业动态 更新时间:2024-10-22 17:32:16
本文介绍了ASP MVC消息处理VS网页API消息处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了2个项目:

  • 普通,基本的ASP.NET MVC应用程序4
  • 基本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 (localhost:4189/Something) the breakpoint is not triggered. When I call Web API however (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 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消息处理程序

    本文发布于:2023-10-13 00:04:33,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1486250.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:消息   网页   程序   ASP   MVC

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!