Asp.net MVC中Route的理解
2024-09-26 06:26:11
在Asp.net MVC中,对于一个请求,是通过路由找到对应的Controller类中的对应Action,并与model进行交互,最后返回到view。
就是说Asp.net MVC是用Route来定义各种URL到实际处理程序之间的映射的。
在处理请求之前,在Global.asax.cs文件的Application_Start中注册路由
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
然后在RouteConfig类中
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
应用程序默认为路由增加了2个路由对象:
1. routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 这个路由是用于忽略特殊请求的路由,这个路由忽略所有扩展名为.axd的请求。
2. routes.MapRoute(....);这个路由可以映射任何符合{controller}/{action}/{id}模式的URL,提供了一个默认的参数。
最新文章
- vue-router2.0 组件之间传参及获取动态参数
- golang调用EXE
- kubernetes多节点部署解析
- 基本概率分布Basic Concept of Probability Distributions 5: Hypergemometric Distribution
- 【6_100】Same Tree
- 写了个Linux包过滤防火墙
- 浅谈Javascript 数组与字典
- hdu4431 Mahjong 枚举搜索。。
- CSU 1808 地铁
- BZOJ 1832: [AHOI2008]聚会( LCA )
- Hibernate 配置详解(12) 补充
- JS 修改元素
- 浅谈html入门
- UUID错误
- Java获得系统的外网IP
- An internal error occurred during: ";Retrieving archetypes:";. GC overhead limit exceeded
- 用C语言做一个横板过关类型的控制台游戏
- git tag 打标签
- datatables后端分页
- vue(v-html)和scss的使用问题
热门文章
- Linux命令总结_文件查找之grep
- 提升 web 应用程序的性能(一)
- 加载外部JavaScript的最佳方法
- 将服务器上的某些特定日志统一保存至XPS文件
- Scrum会议10(Beta版本) 补交
- NOJ 1651 Red packet(二分)
- C++ 中的模板类声明头文件和实现文件分离后,如何能实现正常编译?
- Android消息处理机制
- Maven问题总结:could not resolve archetype xxxxxxx from any of the configured repositories
- [ZZ] HDR the bungie way