簡體   English   中英

從DNX遷移到ASP.NET Core 2.0時API丟失/更改

[英]Missing/changed API when migrating from DNX to ASP.NET Core 2.0

我一直在努力將應用程序從過時的DNX遷移到ASP.NET Core 2.0。 這樣做時,發現名稱空間和API(例如Microsoft.AspNetMicrosoft.AspNetCore幾乎沒有變化。 盡管我已經能夠找到並修復大多數更改,但是以下更改對我造成了問題:

在從Route繼承的類中,在RouteAsync(RouteContext context)方法中,對於DNX,它是context.IsHandled = true; RouteAsync(RouteContext context) ,如何表示這已由ASP.NET Core 2.0處理?

我試圖從GitHub查找更改歷史記錄,但似乎與此無關。

不再需要從RouteAsync調用context.IsHandled 如果您不帶Task return ,則框架知道跳到下一條路線。 如果返回任務,則框架將處理(處理)該任務。

舊版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更新): 啟動后更改MVC6的路由集合

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM