ASP.NET MVC如何设置路由启动

怎么设置DeBug的时候直接启动页面




长风秋雁
浏览 227回答 3
3回答

BIG阳

右键你的mvcwebsite项目属性找到WEB一项选择启动URL,输入你要启动的URL就行了,比如/Home/Index或者找你的项目配置文件如MvcWebSite.csproj.user去找 <WebProjectProperties>节点下的<StartPageUrl>节点里面改成你想启动的路径就行了如<StartPageUrl>/Home/Index</StartPageUrl>

跃然一笑

&nbsp;设置路由启动 还是 设置直接启动页面;我这里的MVC版本是VS2010的mvc2,其他的版本我不是很清楚,但是我觉得应该也差不多;1.在vs2010lz你新建了一个MVC项目后,你会在项目中发现一个Global文件,这个就是决定你所有页面如何访问的关键文件,传统的asp项目是通过访问页面,可以说是访问文件的方式,你可以看到所有的网址后面都是.aspx,但是在mvc中完全不一样了,mvc封装了这种访问,那么lz你只要在这个文件配置就好了;2.在这个文件下有两个方法public static void RegisterRoutes(RouteCollection routes){routes.IgnoreRoute("{resource}.axd/{*pathInfo}");routes.MapRoute(null, // Route name"{控制器名}/{action结果界面}/{参数}", // URL with parametersnew { controller = "Home", action = "index", 参数 = "" });// Parameter defaults// 第一个路由routes.MapRoute(null, // 路由名称"Home/Close/{message}", // 带有参数的 URLnew { controller = "Home", action = "Close", message = "" }); // 参数默认值routes.MapRoute("Default", // 路由名称"{controller}/{action}/{id}", // 带有参数的 URLnew { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值);}这个方法就是你的路由配置,根据你的项目中有哪些action(带参数的,就是你传过去的值),我这里给出了几种不同的配置,lz你可以试试,就应该知道了;3.路由的启动,就是这个文件的另外的方法protected void Application_Start(){AreaRegistration.RegisterAllAreas();........RegisterRoutes(RouteTable.Routes);//路由启动}RegisterRoutes这个就会告诉MVC应用程序,路由注册,每次产生访问请求后,MVC就会在这个方法(就是方法2中的那个方法),寻找你配置的路由,产生返回结果;4.知道上面的基本知识后,那个路由启动就知道了,当MVC启动后他会找到你路由配置的第一个配置, public static void RegisterRoutes(RouteCollection routes)就是这个方法中LZ写的第一个routes.MapRoute,一般的MVC是new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值 这样翻译过来,就是说第一个路由是view下面的Home文件夹,下面的index页面;

猛跑小猪

楼主可以在global里做如下设置public class MvcApplication : System.Web.HttpApplication{public static void RegisterRoutes(RouteCollection routes){routes.IgnoreRoute(".axd/");routes.MapRoute("xiaohong", // Route name"Home/xiaohong", // URL with parametersnew // Parameter defaults);routes.MapRoute("chenghong", // Route name"Home/chenghong", // URL with parametersnew // Parameter defaults);routes.MapRoute( //注意Default一定要写到最后"Default", // Route name"//", // URL with parametersnew // Parameter defaults);}protected void Application_Start(){AreaRegistration.RegisterAllAreas();RegisterRoutes(RouteTable.Routes);}}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP