0

I am new in ASP.Net core and migrating framework code into asp.net core but not understanding how to convert below code into asp.net core.

HttpContext.RewritePath(HttpContext.Request.RawUrl.Substring(0, HttpContext.Request.RawUrl.IndexOf("?", StringComparison.Ordinal)), string.Empty, queryString);

Pankaj
  • 19
  • 2
  • 1
    Could you please describe it clear about what feature you want to achieve? – Anduin Xue Jun 03 '21 at 07:41
  • Why was this code used in the first place? This was in WebForms before routing was introduced and probably isn't needed at all – Panagiotis Kanavos Jun 03 '21 at 07:42
  • The section [ASP.NET Routing versus URL Rewriting](https://learn.microsoft.com/en-us/previous-versions/aspnet/cc668201(v=vs.100)#aspnet-routing-versus-url-rewriting) explains how routing replaced URL rewriting. WebForms couldn't handle parameters in the path. All the frameworks after it do. Instead of having to rewrite `/Products/Widgets/` to `/Products.aspx?id=4`, all frameworks since MVC will forward the call to `ProductsController` or `ProductsPage` (for Razor Pages) and pass `Widgets` as a parameter to he `Get` action. – Panagiotis Kanavos Jun 03 '21 at 07:49
  • URL rewriting was used to strip session IDs from the URL too. Such session IDs aren't used any more. – Panagiotis Kanavos Jun 03 '21 at 07:52
  • You can't just take a WebForms application and recompile it for .NET Core. The architecture of web apps has changed completely in the last 10 years, and many of the quirks and workarounds of WebForms no longer apply – Panagiotis Kanavos Jun 03 '21 at 07:54

1 Answers1

0

Rewriting the path in ASP.NET Core is as simple as running a middleware very early in the pipeline that manipulated the relevant parts of the http request. You can tweak Path, PathBase, QueryString etc and affect the middleware that runs after the rewrite code. This is what the rewrite middleware does that ships as part of ASP.NET Core.

davidfowl
  • 37,120
  • 7
  • 93
  • 103