简体   繁体   中英

How to rewrite url asp.net core

using asp.net core, and I'm trying to rewrite the url from : "product/index/region/type" To : "products/region-type"

region and type are words that are not fixed. Rather, they refer to the region, whatever its name, and also the type

If I understand your requirement correct , then you should use middleware.

Try this:

public class UrlRewriterMiddleware
{
    private readonly RequestDelegate _next;

    public SelfRequestRouterMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        if(context.Request.Path.Value.Contains("product/index/region/type")
               context.Request.Path = context.Request.Path.Value.Replace("product/index/region/type", "products/region-type");

        await _next.Invoke(context);
    }
}

and then in your startup:

app.UseMiddleware<UrlRewriterMiddleware>();

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