2

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.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
RohitWagh
  • 1,999
  • 3
  • 22
  • 43
  • Have you taken a look on [apisof.net](https://apisof.net)? It's a search system, created by Microsoft, which was designed to help developers figure out which namespace certain APIs are in. I did a quick search, and I believe [this](https://apisof.net/catalog/Microsoft.AspNetCore.Routing.RouteBase.RouteAsync(RouteContext)) is the RouteAsync that you are looking for, but I might be wrong. – Jamie Taylor Feb 14 '18 at 14:01
  • @JamieTaylor Thanks for the link. While the link is informative, in current case, it is not that helpful as i wanted to know the replacement/work around for `IsHandled` in .net core 2.0 – RohitWagh Feb 14 '18 at 14:12
  • I see. Apologies. I thought that it would have helped tracking down the answer to your question. – Jamie Taylor Feb 14 '18 at 14:16
  • Depending on what you're doing with your request pipeline, you might not have to use `IsHandled` (if MVC is the last thing added to your pipeline) – Jamie Taylor Feb 14 '18 at 14:17

1 Answers1

3

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

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • Thanks!. **skip to the next route if you return with no Task. If you return a task, then the framework will process (handle) it.** - this is what i was looking for. – RohitWagh Feb 15 '18 at 05:26