asp.net mvc3如何自定义路由
1个回答
展开全部
在Global.asax文件里面添加自己的路由。
参考代码
using System.Web;
using 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());
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询