在.NET Core 3.1中重定向Razor页面

huangapple go评论67阅读模式
英文:

Redirecting Razor page in .NET Core 3.1

问题

我在我的页面文件夹中有一个Razor页面,公共类名为DoStuffModel:PageModel。我想让我的域名/do_stuff访问到我的页面。

我尝试过将以下内容添加到启动文件中:

endpoints.MapControllerRoute(
        name: "do_stuff",
        pattern: "do_stuff",
        defaults: new { page = "/DoStuff" });

还尝试了其他一些方法。这不应该那么难。我已经将其作为do_stuffModel工作,但那是一个糟糕的名称。

英文:

I have a Razor page public class DoStuffModel : PageModel in my Pages folder. I want mydomain/do_stuff to hit my page.

I have tried adding this to startup

endpoints.MapControllerRoute(
        name: "do_stuff",
        pattern: "do_stuff",
        defaults: new { page = "/DoStuff" });

and a few other things. This can't be that hard. I have it working as do_stuffModel, but that is a terrible name.

答案1

得分: 1

为此,请在ConfigureServices方法中添加以下服务:

services.AddRazorPages();

接下来,添加此中间件:

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
});

现在,您可以使用Razor Page,但如果您想在其前面添加路径,您可以在页面文件夹中添加文件夹,例如mydomain/newfolder/mypages

英文:

For this first add this service in ConfigureServices method:

services.AddRazorPages();

Next add this middleware

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
});

Now you can use Razor Page but if you want to add path before it you can add folder in pages folder like this mydomain/newfolder/mypages

huangapple
  • 本文由 发表于 2023年7月14日 01:46:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定