locked
UseExceptionHandler and SlugRouteValueTransformer RRS feed

  • Question

  • User818337214 posted

    Hi,

    I have a class that SlugRouteValueTransformer. If I use return null , UseExceptionHandler is not working 

    public class SlugRouteValueTransformer : DynamicRouteValueTransformer
        {
            public SlugRouteValueTransformer()
            {
            
            }
            public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
            {
                ....
                if(null)
                return new RouteValueDictionary { { "area", "" }, { "controller", "Home" }, { "action", "Error" } };
            }
        }

    Thursday, November 19, 2020 12:43 PM

All replies

  • User1312693872 posted

    Hi,suat_suphi

    Have you defined the followings in your startup.cs?

    public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllersWithViews();
                services.AddScoped<SlugRouteValueTransformer>();
            }
    app.UseEndpoints(endpoints =>
                {
                    endpoints.MapDynamicControllerRoute<SlugRouteValueTransformer>("/Slug");
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");
                });

    Then if I direct to /Slug which is a null page, it will go to the error page as defined:

    public class SlugRouteValueTransformer : DynamicRouteValueTransformer
        {
            public SlugRouteValueTransformer()
            {
    
            }
            public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
            {
               return new RouteValueDictionary { { "area", "" }, { "controller", "Home" }, { "action", "Error" } };
            }
        }

    Best Regards,

    Jerry Cai

    Friday, November 20, 2020 3:17 AM
  • User818337214 posted

    Yes, I have defined the followings

     public void ConfigureServices(IServiceCollection services)
    {
            services.AddScoped<SlugRouteValueTransformer>();
    }
    

    and

    app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllerRoute(
                       name: "areas",
                       pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
    
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");
    
                    endpoints.MapRazorPages();
    
                    endpoints.MapDynamicControllerRoute<SlugRouteValueTransformer>("/{**slug}");
    
                });

    Saturday, November 21, 2020 5:38 PM
  • User1312693872 posted

    Hi,suat_suphi

    How did you 'use return null' ? I tested it by directing to a null page and then it showed the error page as designed. So I guess the problem may

    lies there.

    Best Regards,

    Jerry Cai.

    Tuesday, November 24, 2020 6:06 AM
  • Thursday, November 26, 2020 9:15 PM
  • User1312693872 posted

    Hi,suat_suphi

    Do you want to make all the undesigned page direct to an error page? If so, you do not need to commented out that line.

    public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
            {
                var requestPath = httpContext.Request.Path.Value;
                if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
                    requestPath = requestPath.Substring(1);
                var segments = requestPath.Split('/');
                var url = segments[0].ToLower();
                if (string.IsNullOrEmpty(url))
                    return null;
    
                if (segments.Length == 1)
                {
                    if (url == "test")
                        return new RouteValueDictionary { { "area", "" }, { "controller", "Home" }, { "action", "Test" } };
                }
               return new RouteValueDictionary { { "area", "" }, { "controller", "Home" }, { "action", "Error" } };
            }

    If I have misunderstood anything , welcome to correct me.

    Best Regards,

    Jerry Cai

    Friday, November 27, 2020 6:59 AM
  • User818337214 posted

    hi, Jerry Cai

    thank you for your interest.

            public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
            { 
    
    
                return null;
            }

    https://localhost:5001/aaaaa

    just use  it that one step path /aaa

    Friday, November 27, 2020 10:14 AM
  • User1312693872 posted

    Hi,suat_suphi

    By default, an ASP.NET Core app doesn't provide a status code page for HTTP error status codes, such as 404 - Not Found.  But you can also use 

    app.Use(async (context, next) =>
                {
                    await next();
                    if (context.Response.StatusCode == 404)
                    {
                        context.Request.Path = "/Home/Error";
                        await next();
                    }
                });

    to handle 404, put it in Configure().

    And this link has more details,you can refer to it.

    Best Regards,

    Jerry Cai

    Monday, November 30, 2020 2:47 AM
  • User818337214 posted

    thank you... 

    my expectation is about UseExceptionHandler. It is not working when I use  MapDynamicControllerRoute

    Monday, November 30, 2020 10:46 PM