locked
Url rewriting in Asp.net core 3.1 RRS feed

  • Question

  • User-257070954 posted

    Hi All

                How we implement url rewriting asp.net core 3.1. Below giving my code

    Configure method in Startup.cs

    var rewrite = new RewriteOptions()
    .AddRewrite(@"Bill", "Product/Bill", skipRemainingRules:false);

    Added one action Product Controller

    public IActionResult Bill()
    {
    return View();
    }

    In browser tried below url

    http://localhost:56394/Bill

    I am getting below message in browser

         No webpage was found for the web address: http://localhost:56394/Bill.

      Please let me know where i am doing wrong

    Monday, July 20, 2020 8:38 AM

Answers

  • User-217098811 posted

    Hi binustrat

    In fact, the code you wrote is correct, whether it is true or false,.

    After you write the middleware in app.UseRouting();, rewriting will not be performed, you can write it above app.UseRouting();

    Regards

    yinqiu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 20, 2020 9:51 AM

All replies

  • User-217098811 posted

    Hi binustrat,

    You can try this code:

     var rewrite = new RewriteOptions()
                .AddRewrite(@"Bill", "Product/Bill", skipRemainingRules:true);
                app.UseRewriter(rewrite);

    Hope this can help you.

    Regards

    yinqiu

    Monday, July 20, 2020 9:31 AM
  • User-257070954 posted

    Hi Yinqiu,

                   Thanks for you response. But i am getting same response,


    Below Giving my entire configure method in startup.cs

     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                }
                app.UseStaticFiles();


                app.UseRouting();
                app.UseSession();
                // app.UseAuthorization();
                var rewrite = new RewriteOptions()
                 .AddRewrite(@"Bill", "Product/Bill", true);
                app.UseRewriter(rewrite);


                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");
                   
                });
            }

    Monday, July 20, 2020 9:43 AM
  • User-217098811 posted

    Hi binustrat

    In fact, the code you wrote is correct, whether it is true or false,.

    After you write the middleware in app.UseRouting();, rewriting will not be performed, you can write it above app.UseRouting();

    Regards

    yinqiu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 20, 2020 9:51 AM