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


    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}";
            }
  • 相关阅读:
    前端页面适配的rem换算
    Win10远程桌面 出现 身份验证错误,要求的函数不受支持,这可能是由于CredSSP加密Oracle修正 解决方法
    ES5, ES6, ES2016, ES.Next: What's going on with JavaScript versioning?
    国内的Android SDK镜像
    虚拟串口VSPD破解版 亲测win10 64可用
    Mybatis : "less than" issue in Select annotations
    如何在MyBatis中优雅的使用枚举
    Adding a custom jar as a maven dependency
    Error: Invalid or corrupt jarfile
    使用Json让Java和C#沟通的方法
  • 原文地址:https://www.cnblogs.com/jiangcm/p/11299375.html
Copyright © 2020-2023  润新知