简体   繁体   中英

ASP.NET CORE 3.1 Custom Razor Page Routing for Customers

I have a asp.net web forms I am converting to .NET CORE. The website has many users managing multiple customers in the system. The users need to be able to manage customers from multiple browser tabs. My web old application simply passed the parameter CustomerId, and I had to manage the CustomerId value across multiple pages which was a pain. All pages had to use the query ?CustomerId=XXX

In ASP.net Core 3, is there is simple way to do this in routes and construct URLs? I would like to add a route that looks like this?

  1. http://mywebsite.com/customer/XXX/SettingsPage this would route to a razor page /Pages/SettingsPage.cshtml

  2. http://mywebsite.com/customer/XXX/AdminArea/Users/Edit1 this would route to an area with a razor page /Areas/AdminArea/Users/Edit.cshtml

Then in my code I can create URLs from the route and fetch the XXX value and query the customer table.

After reading this post Tab specific cookies without using sessionStorage or any HTML5 features

I used the URL rewrite engine in asp.net core. There is the sample project found here https://github.com/aspnet/AspNetCore.Docs/tree/master/aspnetcore/fundamentals/url-rewriting/samples/3.x/SampleApp

using (StreamReader apacheModRewriteStreamReader =
                File.OpenText("ApacheModRewrite.txt"))
            using (StreamReader iisUrlRewriteStreamReader =
                File.OpenText("IISUrlRewrite.xml"))
            {
                var options = new RewriteOptions()
                    .AddRedirect("redirect-rule/(.*)", "redirected/$1")
                    .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2", skipRemainingRules: true)
                    .AddRewrite(@"customer(\d+)\/(.*)$", "$2?customerid=$1", skipRemainingRules: true)
                    .AddApacheModRewrite(apacheModRewriteStreamReader)
                    .AddIISUrlRewrite(iisUrlRewriteStreamReader)
                    .Add(RewriteRules.MethodRules.RedirectXmlFileRequests)
                    .Add(RewriteRules.MethodRules.RewriteTextFileRequests)
                    .Add(new RewriteRules.RedirectImageRequests(".png", "/png-images"))
                    .Add(new RewriteRules.RedirectImageRequests(".jpg", "/jpg-images"));

                app.UseRewriter(options);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM