简体   繁体   English

剃刀页面-从旧页面路由到新页面

[英]Razor Pages - Routing from old page to new one

I'm using "razor pages" with asp.net core. 我在asp.net核心中使用“剃须刀页面”。

In one of my pages called "MyNewPage" I have the following piece of code: 在我的一个名为“ MyNewPage”的页面中,有以下代码:

    public void OnGetTest(string MyParam)
    {
       //do something
    }

To execute it I simply go to: /MyNewPage?handler=Test&MyParam=abc 要执行它,我只需转到:/ MyNewPage?handler = Test&MyParam = abc

This works perfectly fine. 这工作得很好。 What I would like to do now is to map an old URL to it. 我现在想做的是将一个旧的URL映射到它。 For example: 例如:

     /MyOldPage/xyz should map to  /MyNewPage?handler=Test&MyParam=xyz

In Startup.cs I would like to write something like this in ConfigureServices but it doesn't work. 在Startup.cs中,我想在ConfigureServices中编写类似的内容,但是它不起作用。 Any ideas how I can achieve this? 有什么想法可以实现这一目标吗?

     options.Conventions.AddPageRoute("/MyNewPage?handler=Test&MyParam={text}", "/MyOldPage/{text}");

Consider using Url Rewriting Middleware . 考虑使用URL重写中间件 Here what the code in your Startup.Configure method will look like: 这里,您的Startup.Configure方法中的代码将如下所示:

var options = new RewriteOptions();
options.AddRewrite("MyOldPage/(.*)", "MyNewPage/?handler=Test&MyParam=$1", false);
app.UseRewriter(options);

You should tweak the regular expression (.*) as necessary, to match to pattern or the URL to redirect from. 您应该根据需要调整正则表达式(.*) ,以匹配模式或要重定向的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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