如何配置ASP.NET MVC Route的多种路由方式?

2026-03-30 11:411阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计183个文字,预计阅读时间需要1分钟。

如何配置ASP.NET MVC Route的多种路由方式?

1. 利用httpContext.Request.UserAgent(简称UA)来过滤传递的Url请求,在RegisterRoutes中添加如下路由:csharproutes.Add(chrome, new ElevenRoute());

public class ElevenRoute : RouteBase{ // 扩展这个类 public override RouteData GetRouteData(HttpContextBase httpContext) { // 实现路由逻辑 }}

1.使用localhost:8088/Brad的时候,匹配的还是localhost:8088/Home)

routes.MapRoute( name: "Brad", url: "Brad/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );//1 匹配以Brad开头的地址,控制器就用Home

4.写死了WYD就访问Home/About

routes.MapRoute( name: "WYD", url: "WYD", defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional } );

5.从Url中直接读取年月日参数

如何配置ASP.NET MVC Route的多种路由方式?

routes.MapRoute( name: "Regex", url: "{controller}/{action}_{Year}_{Month}_{Day}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, constraints: new { Year = @"^\d{4}", Month = @"^\d{2}", Day = @"^\d{2}", } );

public string Format(int year, int month, int day) { return $"This is {year}-{month}-{day}"; }

本文共计183个文字,预计阅读时间需要1分钟。

如何配置ASP.NET MVC Route的多种路由方式?

1. 利用httpContext.Request.UserAgent(简称UA)来过滤传递的Url请求,在RegisterRoutes中添加如下路由:csharproutes.Add(chrome, new ElevenRoute());

public class ElevenRoute : RouteBase{ // 扩展这个类 public override RouteData GetRouteData(HttpContextBase httpContext) { // 实现路由逻辑 }}

1.使用localhost:8088/Brad的时候,匹配的还是localhost:8088/Home)

routes.MapRoute( name: "Brad", url: "Brad/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );//1 匹配以Brad开头的地址,控制器就用Home

4.写死了WYD就访问Home/About

routes.MapRoute( name: "WYD", url: "WYD", defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional } );

5.从Url中直接读取年月日参数

如何配置ASP.NET MVC Route的多种路由方式?

routes.MapRoute( name: "Regex", url: "{controller}/{action}_{Year}_{Month}_{Day}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, constraints: new { Year = @"^\d{4}", Month = @"^\d{2}", Day = @"^\d{2}", } );

public string Format(int year, int month, int day) { return $"This is {year}-{month}-{day}"; }