当前位置 : 主页 > 网络编程 > ASP >

ASP.NET MVC Route的几种配置方法

来源:互联网 收集:自由互联 发布时间:2021-06-24
1.使用httpContext.Request.UserAgent(俗称UA)来过滤传进来的Url请求,在RegisterRoutes里Add如何路由: routes.Add("chrome", new ElevenRoute()); public class ElevenRoute : RouteBase // 扩展这个类 { public override Ro

1.使用httpContext.Request.UserAgent(俗称UA)来过滤传进来的Url请求,在RegisterRoutes里Add如何路由:

  routes.Add("chrome", new ElevenRoute());

public class ElevenRoute : RouteBase //扩展这个类
    {
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            if (httpContext.Request.UserAgent.IndexOf("Chrome/") >= 0)//通过拿到http的上下文,为所欲为,判断请求进来的浏览器版本,来过滤用户请求等等。
            {
                return null;
            }
            else
            {
                RouteData routeData = new RouteData(this, new MvcRouteHandler());
                routeData.Values.Add("controller", "Home");
                routeData.Values.Add("action", "Refuse");
                return routeData;
            }
        }
        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            return null;
        }
    }

2.使用固定的Url访问服务器上的文件,一般用于不给员工开访问服务器的权限,但是要查看服务器上的日志,怎么办。

routes.Add("Eleven", new Route("Eleven/201903042135", new ElevenRouteHandler()));

public class ElevenRouteHandler : IRouteHandler //扩展RouteHandler
    {
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return new ElevenHttpHandler();
        }
    }
    public class ElevenHttpHandler : IHttpHandler
    {
        public bool IsReusable => true;

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            //context.Response.Write("This is Eleven‘s Framework");
            string text = File.ReadAllText(context.Server.MapPath("~/Web.Config"));
            context.Response.Write(text);
            context.Response.End();
            //context.Response.Write($"Now is {DateTime.Now.ToShortTimeString()}");
        }
    }

3.匹配以Brad开头的地址,控制器就用Home(defaults的作用就是,当你输入http://localhost:8088/Brad的时候,匹配的还是http://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中直接读取年月日参数

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}";
        }
网友评论