请教个MVC的控件器与视图的问题

月光下的土豆 2016-08-09 04:25:49
MVC项目通过浏览器访问是
http://***/控制器名/方法名 这样的方式来访问的。
方法名通常对应一个同名的cshtml文件。

今天学习一个项目。
控制器中没有 index这个方法,view 中有 index.cshtml 这个视图文件。
通过 http://***/控制器名/index 仍可以访问。
求解,这是怎么回事?
...全文
149 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 版主 2016-08-09
  • 打赏
  • 举报
回复
或许人家使用了 [ActionName("Index")] 起了个别名?
月光下的土豆 2016-08-09
  • 打赏
  • 举报
回复
还是没有怎么明白, 两个路由表

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Default",                                                                      //路由名称
                "{controller}/{action}/{id}",                                                   //带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }       //参数默认值
            );
        }
Areas 内的,如下

 public class BaseInformationAreaRegistration : AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "BaseInformation";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) 
        {
            /*context.MapRoute(
                "BaseInformation_default",
                "BaseInformation/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );*/
            context.MapRoute(
                this.AreaName + "_Default",
                this.AreaName + "/{controller}/{action}/{id}",
                new { area = this.AreaName, controller = "Home", action = "Index", id = UrlParameter.Optional },
                new string[] { "WebApp.Areas." + this.AreaName + ".Controllers" }
            );
        }
    }
  • 打赏
  • 举报
回复
所谓的URL其实是由RouteProvider实现的,通常在Global.asax中注册,这是基础知识,大多数书上都有介绍 如果有.net基础,看看MSDN就可以了

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧