Missing/changed API 从 DNX 迁移到 ASP.NET Core 2.0
Missing/changed API when migrating from DNX to ASP.NET Core 2.0
我一直致力于将应用程序从过时的 DNX 迁移到 ASP.NET Core 2.0。在这样做的同时,它发现命名空间和 API 几乎没有变化,例如 Microsoft.AspNet
到 Microsoft.AspNetCore
。虽然我已经能够找到并修复大部分更改,但以下更改对我造成了问题:
在从 Route
继承的 class 中,在 RouteAsync(RouteContext context)
方法中,使用 DNX 时是 context.IsHandled = true;
,如何表示现在已使用 ASP.NET 核心 2.0?
我试图从 GitHub 中查找更改历史记录,但似乎有 none 与此相关。
不再需要从 RouteAsync
调用 context.IsHandled
。如果你 return
而没有 Task
,框架知道跳到下一条路线。如果你return一个任务,那么框架会处理(handle)它。
旧 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 更新):
我一直致力于将应用程序从过时的 DNX 迁移到 ASP.NET Core 2.0。在这样做的同时,它发现命名空间和 API 几乎没有变化,例如 Microsoft.AspNet
到 Microsoft.AspNetCore
。虽然我已经能够找到并修复大部分更改,但以下更改对我造成了问题:
在从 Route
继承的 class 中,在 RouteAsync(RouteContext context)
方法中,使用 DNX 时是 context.IsHandled = true;
,如何表示现在已使用 ASP.NET 核心 2.0?
我试图从 GitHub 中查找更改历史记录,但似乎有 none 与此相关。
不再需要从 RouteAsync
调用 context.IsHandled
。如果你 return
而没有 Task
,框架知道跳到下一条路线。如果你return一个任务,那么框架会处理(handle)它。
旧 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 更新):