分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

MVC_Route层层深入

发布时间:2023-09-06 02:01责任编辑:彭小芳关键词:MVC

1.简介

  新建一个MVC项目,并添加Home和About两个控制器

  在这两个控制器对应添加index页面

namespace Study_MVC_Route.Controllers{ ???public class HomeController : Controller ???{ ???????// GET: Home ???????public ActionResult Index() ???????{ ???????????return View(); ???????} ???????public string Regex(int year,int month,int day) ???????{ ???????????return $"{year}-{month}-{day}";
       //$是在字符串中内嵌参数 ???????} ???}}
namespace Study_MVC_Route.Controllers{ ???public class AboutController : Controller ???{ ???????// GET: About ???????public ActionResult Index() ???????{ ???????????return View(); ???????} ???}}

  在APP_Start文件夹下面的RouteConfig.cs,是MVC的路由配置文件

  主要属性如下:

 ?name: 路由名称 ?url: 和url匹配的正则表达式,用{}表示一个参数变量,可以为空;无{}则是一个硬性的匹配要求 ?defaults: 默认,匹配上url后,若url参数为空,则使用默认动作 ??controller = 默认控制器, action = 默认动作, id = 参数
 constraints: url参数约束
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Web.Routing;namespace Study_MVC_Route{ ???public class RouteConfig ???{ ???????public static void RegisterRoutes(RouteCollection routes)//路由集,由上往下匹配路由 ???????{ ???????????routes.IgnoreRoute("{resource}.axd/{*pathInfo}");//忽略路由
???????????//--------------------------------添加路由------------------------------------------ ???????????//以Test开始的url
???????????//url没有引入controller参数,所以controller使用默认值,若不给参数赋值,也使用默认值
 ???????????routes.MapRoute( ???????????????name: "Test", ???????????????url: "Test/{action}/{id}", ???????????????defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ???????????);





        ???????????//Route的书写可以省略命名参数,即name、url、defaults这些命名参数可以省略,如下 ???????????//url为About可以访问Home/Index ???????????routes.MapRoute("About1", "About", new { controller = "Home", action = "Index", id = UrlParameter.Optional }); ???????????//url为About访问About/Index ???????????routes.MapRoute("About2", "{controller}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
       //加大括号表示参数,不加则为一个普通字符串链接,是一个硬性的匹配要求







       //传参对比 ???????????//使用constraints做参数约束 ???????????//home/Regex_2014_05_19 ???????????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}" }// @"\d{4}"表示输入四位数字,@表取消\的转译效果 ???????????);       //普通传参 ???????????//home/Regex?year=2014&month=05&day=19 ???????????routes.MapRoute( ???????????????name: "Default", ???????????????url: "{controller}/{action}/{id}", ???????????????defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ???????????); ???????} ???}}

MVC_Route层层深入

原文地址:https://www.cnblogs.com/wskxy/p/9217057.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved