MVC路由Route
来源:互联网 发布:美利达勇士300d淘宝 编辑:程序博客网 时间:2024/06/02 01:31
控制器方法:
public ActionResult Index(string controller, string action, int id) { ViewBag.Name1 = controller; ViewBag.Name2 = action; ViewBag.Name3 = id; return View(); }
一、MVC新建项目后会有一个默认路由:
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
那么访问地址为:http://localhost:22631/home/index/1
二、添加一个自定义路由,记得自定义路由一定要放在默认路由上边,放置顺序很重要
routes.MapRoute( name: "DefaultX", url: "X{controller}/{action}/{id}", namespaces: new string[] { "IceCreamRouteTest.Controllers" }, defaults: new { id = UrlParameter.Optional } );那么访问地址为:http://localhost:22631/xhome/index/1
三、自定义路由
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, httpMethod = new HttpMethodConstraint("POST") }, namespaces: new string[] { "IceCreamRouteTest.Controllers" }//, , constraints: new { customConstraint2222 = new MyRouteConstraint("Chrome") } //constraints: new { id = @"\d+" } //constraints: new { id = new IntRouteConstraint() } //,constraints: new MyRouteConstraint("Chrome") //, constraints: new { controller = new MyRouteConstraint("Chrome") } //OK );
namespace IceCreamRouteTest.Models.Filters{ public class MyRouteConstraint : IRouteConstraint { private string requiredUserAgent; public MyRouteConstraint(string agentParam) { requiredUserAgent = agentParam; } public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { return httpContext.Request.UserAgent.Contains(requiredUserAgent); } }}
阅读全文
0 0
- MVC-路由route
- MVC路由Route
- route 路由
- route路由
- linux 路由 route命令
- linux 路由route
- 路由修改route
- 路由修改 route
- IP-route管理路由
- linux路由route配置
- 路由映射Route
- route设置路由
- linux route路由
- angularJs-route路由详解
- AngularJS $route路由
- angularJS 路由 ng-route
- YII2路由(Route)
- Flask 路由 route
- Android 系统提示对话框(AlertDialog)的使用
- SCUT Training 20170913 Problem H
- Check Point R80 Security Management
- 各hbase版本对hadoop各版本的支持情况
- 二叉搜索树的后续遍历序列
- MVC路由Route
- URAL
- Andriod项目build.gradle文件详解
- centos下git的安装以及代码自动同步
- 网易春招[编程题]调整队形
- 老司机带你玩转git(二)git的分支特性
- mysql数据库执行mysqladmin flush-hosts方法
- Ubuntu React Native环境搭建
- json