【问题标题】:Missing/changed API when migrating from DNX to ASP.NET Core 2.0从 DNX 迁移到 ASP.NET Core 2.0 时缺少/更改 API
【发布时间】:2018-02-14 13:27:34
【问题描述】:

我一直致力于将应用程序从过时的 DNX 迁移到 ASP.NET Core 2.0。在这样做的同时,它发现命名空间和 API 几乎没有变化,例如 Microsoft.AspNetMicrosoft.AspNetCore。虽然我已经能够找到并修复大部分更改,但以下一项对我来说是个问题:

在继承自Route 的类中,在RouteAsync(RouteContext context) 方法中,使用DNX 时它是context.IsHandled = true;,我如何表示现在已经使用ASP.NET Core 2.0 处理了这个问题?

我试图从 GitHub 中查找更改历史记录,但似乎没有与此相关的内容。

【问题讨论】:

  • 你看过apisof.net吗?这是一个搜索系统,由 Microsoft 创建,旨在帮助开发人员找出某些 API 所在的命名空间。我进行了快速搜索,我相信 this 是您正在寻找的 RouteAsync,但我可能错了.
  • @JamieTaylor 感谢您的链接。虽然该链接提供了丰富的信息,但在当前情况下,它并没有那么有用,因为我想知道 .net core 2.0 中 IsHandled 的替换/解决方法
  • 我明白了。道歉。我认为这将有助于找到您问题的答案。
  • 根据您对请求管道所做的工作,您可能不必使用 IsHandled(如果 MVC 是添加到管道中的最后一件事)

标签: c# asp.net-core .net-core asp.net-core-2.0 dnx


【解决方案1】:

不再需要从RouteAsync 调用context.IsHandled。如果您return 没有Task,则框架知道跳到下一条路线。如果您返回一个任务,那么框架将处理(处理)它。

旧 DNX / MVC6 / 预览代码

public async Task RouteAsync(RouteContext context)
{
    var requestPath = context.HttpContext.Request.Path.Value;

    if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
    {
        // Trim the leading slash
        requestPath = requestPath.Substring(1);
    }

    // Get the page id that matches.
    TPrimaryKey id;

    //If this returns false, that means the URI did not match
    if (!GetPageList().TryGetValue(requestPath, out id))
    {
        return;
    }

    //Invoke MVC controller/action
    var oldRouteData = context.RouteData;
    var newRouteData = new RouteData(oldRouteData);
    newRouteData.Routers.Add(_target);

    newRouteData.Values["controller"] = _controller;
    newRouteData.Values["action"] = _action;

    // This will be the primary key of the database row.
    // It might be an integer or a GUID.
    newRouteData.Values["id"] = id;

    try
    {
        context.RouteData = newRouteData;
        await _target.RouteAsync(context);
    }
    finally
    {
        // Restore the original values to prevent polluting the route data.
        if (!context.IsHandled)
        {
            context.RouteData = oldRouteData;
        }
    }
}

新的 .NET Core 1.x / .NET Core 2.x 代码

public async Task RouteAsync(RouteContext context)
{
    var requestPath = context.HttpContext.Request.Path.Value;

    if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
    {
        // Trim the leading slash
        requestPath = requestPath.Substring(1);
    }

    // Get the page id that matches.
    TPrimaryKey id;

    //If this returns false, that means the URI did not match
    if (!GetPageList().TryGetValue(requestPath, out id))
    {
        return;
    }

    //Invoke MVC controller/action
    var routeData = context.RouteData;

    routeData.Values["controller"] = _controller;
    routeData.Values["action"] = _action;

    // This will be the primary key of the database row.
    // It might be an integer or a GUID.
    routeData.Values["id"] = id;

    await _target.RouteAsync(context);
}

此处的完整代码(针对 .NET Core 1/2 更新):Change route collection of MVC6 after startup

【讨论】:

  • 谢谢!。 如果您没有任务返回,请跳到下一条路线。如果您返回一个任务,那么框架将处理(处理)它。 - 这就是我想要的。
猜你喜欢
  • 1970-01-01
  • 2019-11-12
  • 1970-01-01
  • 2018-01-29
  • 2021-06-04
  • 1970-01-01
  • 2020-07-26
  • 2020-02-19
  • 1970-01-01
相关资源
最近更新 更多