C# 从DNX迁移到ASP.NET Core 2.0时缺少/更改了API
我一直在致力于将一个应用程序从过时的DNX迁移到ASP.NET Core 2.0。在这样做的同时,它发现名称空间和API中几乎没有变化,如C# 从DNX迁移到ASP.NET Core 2.0时缺少/更改了API,c#,asp.net-core,.net-core,asp.net-core-2.0,dnx,C#,Asp.net Core,.net Core,Asp.net Core 2.0,Dnx,我一直在致力于将一个应用程序从过时的DNX迁移到ASP.NET Core 2.0。在这样做的同时,它发现名称空间和API中几乎没有变化,如Microsoft.AspNet到Microsoft.AspNetCore。虽然我已经能够找到并修复大多数更改,但下面的一个更改给我带来了问题: 在继承自Route的类中,在RouteAsync(RouteContext)方法中,使用DNX时为context.IsHandled=true,我如何表示现在已使用ASP.NET Core 2.0处理此问题 我试图从
Microsoft.AspNet
到Microsoft.AspNetCore
。虽然我已经能够找到并修复大多数更改,但下面的一个更改给我带来了问题:
在继承自Route
的类中,在RouteAsync(RouteContext)
方法中,使用DNX时为context.IsHandled=true代码>,我如何表示现在已使用ASP.NET Core 2.0处理此问题
我试图从GitHub中查找更改历史记录,但似乎与此无关。不再需要调用上下文。IsHandled
fromRouteAsync
。如果返回时没有任务
,框架知道跳到下一条路线。如果您返回一个任务,那么框架将处理它
旧DNX/MVC6/预览代码
新的.NET Core 1.x/.NET Core 2.x代码
此处的完整代码(针对.NET Core 1/2进行了更新):您看过了吗?这是一个由微软创建的搜索系统,旨在帮助开发人员找出某些API所在的名称空间。我做了一个快速搜索,我相信这就是你正在寻找的RouteAsync,但我可能错了。@JamieTaylor谢谢你的链接。虽然该链接提供了信息,但在当前情况下,它并没有那么大的帮助,因为我想知道.net core 2.0中IsHandled的替换/解决方法。道歉。我认为这将有助于找到您问题的答案。根据您对请求管道所做的操作,您可能不必使用IsHandled
(如果MVC是最后添加到管道中的内容),谢谢!。如果返回时没有任务,请跳到下一条路线。如果您返回一个任务,那么框架将处理它。-这就是我要找的。
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;
}
}
}
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);
}