惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
V
Visual Studio Blog
宝玉的分享
宝玉的分享
IT之家
IT之家
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
I
InfoQ
B
Blog RSS Feed
T
Threatpost
博客园_首页
M
MIT News - Artificial intelligence
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Know Your Adversary
Know Your Adversary
U
Unit 42
Engineering at Meta
Engineering at Meta
C
Cyber Attacks, Cyber Crime and Cyber Security
月光博客
月光博客
Scott Helme
Scott Helme
T
Tor Project blog
有赞技术团队
有赞技术团队
AWS News Blog
AWS News Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
S
Schneier on Security
Vercel News
Vercel News
博客园 - Franky
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
L
LangChain Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
The GitHub Blog
The GitHub Blog
雷峰网
雷峰网
Latest news
Latest news
C
CXSECURITY Database RSS Feed - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
GRAHAM CLULEY
S
Security Affairs
A
About on SuperTechFans
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
大猫的无限游戏
大猫的无限游戏
W
WeLiveSecurity
Cisco Talos Blog
Cisco Talos Blog
罗磊的独立博客

博客园 - kobe

.Net Razor Ajax Post Get处理 .net 7 session支持 windows redis常用 GO语言Windows安装和VScode配置 ASP.NET CORE 托管IIS第一次访问慢 .netcore发布托管IIS后 swagger显示404 dotnet run .net core web html中午查看源码显示乱码 centos netcore开机启动 net core 6.0 session支持,非controller引用session net core web项目(net 6.0)增加apicontroller JPush,Jverify,JCore Ios冲突 apiRecord:methodName service mysqld restart 无效 Python log Python Flask+Windows Service制作 Windows下删除.svn文件夹 C# LINQ list遍历并组装返回新查询 windows server 2016下360wifi安装 Python获取本机多IP并指定出口IP
NetCore Razor 路由伪静态设置
kobe · 2022-10-12 · via 博客园 - kobe

参考文档:

razor路由: https://www.learnrazorpages.com/razor-pages/routing

netcore 伪静态:https://learn.microsoft.com/zh-cn/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-6.0#rrr

找到3中路由方式

1.program.cs 单一映射,其中伪静态2和3 会冲突,只能路由到一个页面

复制代码

builder.Services.AddRazorPages().AddRazorPagesOptions(options => {
options.Conventions.AddPageRoute("/news/detail", "/news_{id}.html");//伪静态1
options.Conventions.AddPageRoute("/news/list", "/newslist_{id}.html"); //伪静态2
options.Conventions.AddPageRoute("/news/list", "/newslist_{id}_p{pageIndex}.html");//伪静态3
});

复制代码

2.program.cs 页面路由模型约定,可以一对多路由,解决上面的冲突问题

复制代码

//Razor路由映射
builder.Services.AddRazorPages().AddRazorPagesOptions(options => {
//一对多路径
options.Conventions.AddPageRouteModelConvention("/news/list", pm =>
{
var routes = new List<string>()
{
  "/newslist_{id:int}_p{pageIndex:int}.html",
  "/newslist_{id}.html"
};
int i = 0;
routes.ForEach(temp =>
{
  pm.Selectors.Add(new Microsoft.AspNetCore.Mvc.ApplicationModels.SelectorModel()
  {
  AttributeRouteModel = new Microsoft.AspNetCore.Mvc.ApplicationModels.AttributeRouteModel()
  {
    Order = i,
    Name = "news_list_" + i,
    Template = AttributeRouteModel.CombineTemplates("", temp)
  }
  });
i++;
});

});

});

复制代码

3.net core Rewrite https://learn.microsoft.com/zh-cn/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-6.0#rrr

引用Microsoft.AspNetCore.Rewrite

复制代码

using (StreamReader apacheModRewriteStreamReader =
File.OpenText("ApacheModRewrite.txt"))
using (StreamReader iisUrlRewriteStreamReader =
File.OpenText("IISUrlRewrite.xml"))
{
  var options = new RewriteOptions()
  //.AddRedirect("redirect-rule/(.*)", "redirected/$1")
  //.AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2",skipRemainingRules: true)
  .AddApacheModRewrite(apacheModRewriteStreamReader)
  .AddIISUrlRewrite(iisUrlRewriteStreamReader)
  .Add(MethodRules.RedirectXmlFileRequests)
  .Add(MethodRules.RewriteTextFileRequests);
  app.UseRewriter(options);
}

复制代码

附:IISUrlRewrite.xml

复制代码

<rewrite>
<rules>
<rule name="news_detail_id_typeid" stopProcessing="true">
<match url="^news_(.*)_t(.*).html$" />
<action type="Rewrite" url="/news/detail?id={R:1}&amp;typeId={R:2}" appendQueryString="false"/>
</rule>
<rule name="Rewrite segment to id querystring" stopProcessing="true">
<match url="^news_(.*).html$" />
<action type="Rewrite" url="/news/detail?id={R:1}" appendQueryString="false"/>
</rule>
</rules>
</rewrite>