在Global.asax文件里面添加自己的路由。
参考代码
using System.Webusing System.Web.Mvc
using System.Web.Routing
using PersonalWebSite.UI.Common
namespace PersonalWebSite.UI
{
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute())
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
routes.MapRoute(
"ArticleClassification", // 导航路由(带参数)
"article_{classificationId}_{pageCount}_{pageIndex}.html", // 带有参数的 URL
new { controller = "Article", action = "Index", classificationId = "classificationId", pageCount = "pageCount", pageIndex = "pageIndex" } // 参数默认值
)
routes.MapRoute(
"Navigation_Two_Parameter", // 导航路由(带参数)
"{controller}_{pageCount}_{pageIndex}.html", // 带有参数的 URL
new { controller = "Index", action = "Index", pageCount = "pageCount", pageIndex = "pageIndex" } // 参数默认值
)
routes.MapRoute(
"Navigation_One_Parameter", // 导航路由(带参数)
"{controller}_{id}.html", // 带有参数的 URL
new { controller = "Index", action = "Show", id = "id" } // 参数默认值
)
routes.MapRoute(
"Navigation", // 导航路由
"{controller}.html", // 带有参数的 URL
new { controller = "Index", action = "Index", id = UrlParameter.Optional } // 参数默认值
)
routes.MapRoute(
"Admin", // 后台管理路由
"Admin/{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "AdminIndex", action = "Index", id = UrlParameter.Optional } // 参数默认值
)
routes.MapRoute(
"Default", // 默认路由
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "Index", action = "Index", id = UrlParameter.Optional } // 参数默认值
)
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas()
RegisterGlobalFilters(GlobalFilters.Filters)
RegisterRoutes(RouteTable.Routes)
RegisterView()//注册自定义视图访问规则
}
protected void RegisterView()
{
ViewEngines.Engines.Clear()
ViewEngines.Engines.Add(new MyViewEngine())
}
}
}
要几个参数就加几个大括号,{id}/{date},在页面层用“<%$RouteUrl:id=123,date=456%>”
就可以设置多个参数了,
直接用路由url传值,在cs文件中用Page.RouteData.Values["id"]获取id=123
把参数固定可以防止无数据显示的路由,我就是把它固定了存到数据库中,你可以看下http://www.green-batteries.com/products/rechargeable-backup-battery.htm,
改动url后跳传到提示页面了,之前用动态的改了url页面上什么也没有
我不是很清楚LZ你问的是 设置路由启动 还是 设置直接启动页面;我这里的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 parameters
new { controller = "Home", action = "index", 参数 = "" })// Parameter defaults
// 第一个路由
routes.MapRoute(null, // 路由名称
"Home/Close/{message}", // 带有参数的 URL
new { controller = "Home", action = "Close", message = "" })// 参数默认值
routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { 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页面;
lz你还有不懂的话,直接hi上留言;
------原创回答 by 灰小盒
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)