从DNX迁移到ASP.NET Core 2.0时API丢失/更改 [英] Missing/changed API when migrating from DNX to ASP.NET Core 2.0

查看:84
本文介绍了从DNX迁移到ASP.NET Core 2.0时API丢失/更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力将应用程序从过时的DNX迁移到ASP.NET Core 2.0。这样做时,发现名称空间和API几乎没有变化,例如 Microsoft.AspNet Microsoft.AspNetCore 。尽管我已经找到并修复了大多数更改,但以下内容对我造成了问题:

I have been working on migrating an application from obsolete DNX to ASP.NET Core 2.0. While doing so it found that there has been few changes in the namespaces and APIs like Microsoft.AspNet to Microsoft.AspNetCore. Though I have been able to find and fix most of the changes, the below one is causing issue for me:

在继承自 Route ,在 RouteAsync(RouteContext context)方法中,对于DNX,它是 context.IsHandled = true; ,我如何表示这已由ASP.NET Core 2.0处理?

In class inherited from Route, within RouteAsync(RouteContext context) method, with DNX it was context.IsHandled = true;, how do I signify that this has been handled now with ASP.NET Core 2.0?

我试图从GitHub查找更改历史记录,但似乎没有相关关系

I have tried to find change history from GitHub but there seems to be none related to this.

推荐答案

不再需要调用 context.IsHandled 来自 RouteAsync 。如果您返回而没有 Task ,则框架知道跳至下一条路线。如果您返回任务,则框架将对其进行处理(处理)。

It is no longer necessary to call context.IsHandled from RouteAsync. The framework knows to skip to the next route if you return with no Task. If you return a task, then the framework will process (handle) it.

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代码



New .NET Core 1.x / .NET Core 2.x Code

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更新):在启动后更改MVC6的路由集合

Full code here (updated for .NET Core 1/2): Change route collection of MVC6 after startup

这篇关于从DNX迁移到ASP.NET Core 2.0时API丢失/更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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