c# - 在asp.net core 2.0中重写url

标签 c# asp.net-mvc asp.net-core-mvc asp.net-core-2.0

我想重写网址

http://localhost:56713/Home/UserDetails?Code=223322


http://localhost:56713/223322

我在 StartUp.cs 中写了下面的内容,但它不起作用
var rewrite = new RewriteOptions()
  .AddRewrite(@"{$1}", "Home/UserDetails?Code={$1}",true);

最佳答案

您需要在 AddRewrite 函数的第一个参数上使用正则表达式。

var rewrite = new RewriteOptions().AddRewrite(
     @"^Home/UserDetails?Code=(.*)",  // RegEx to match Route
     "Home/{$1}",                     // URL to rewrite route
     skipRemainingRules: true         // Should skip other rules
);

此链接可能有助于提供更多示例
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?tabs=aspnetcore2x

关于c# - 在asp.net core 2.0中重写url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49405470/

相关文章:

c# - 从列表中获取不同计数

c# - 在部分类中设置属性的属性

c# - ASP.NET MVC 3 : Possible to just use the V part?

javascript - 当我在 ASP.NET MVC 2 RC2 中打开客户端验证时,为什么我的所有内容都会消失?

主站点之间的asp.net缓存共享添加其应用程序/虚拟目录(使用iis 7.5)

javascript - 具有.net核心模型的AngularJS未绑定(bind)

c# - 如何访问嵌套在 View 模型中的 ObservableCollection

c# - 用 C++ 开发 Win32 应用程序比用 C# 开发 .NET 应用程序有什么优势?

c# - 如何在 ASP.NET Core MVC 中启用跨源请求 (CORS)

mysql - 无法在 .NET Core 上使用 MiniProfiler 分析 MySQL