问题
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:
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?
I have tried to find change history from GitHub but there seems to be none related to this.
回答1:
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.
Old DNX / MVC6 / Preview 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 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;
}
}
}
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);
}
Full code here (updated for .NET Core 1/2): Change route collection of MVC6 after startup
来源:https://stackoverflow.com/questions/48788390/missing-changed-api-when-migrating-from-dnx-to-asp-net-core-2-0