如果您不需要page=0&IdAgency=2
,至少有 2 个选项:
http://localhost:30120/Agency/AgencyName/2/0
用 url like和 using替换它 MVC.AGENCY.INDEX (string name, int? Page, int IdAgency)
(参见下面路由中的 Way1)
完全从控制器中删除 id 和 page 并仅按名称映射(仅当它是唯一的时)。您将拥有http://localhost:30120/Agency/AgencyName
并使用MVC.AGENCY.INDEX (string name)
(请参阅下面路由中的 Way2)
要拥有 seo 网址,您需要注册路线。你可以在Application_Start
方法中做到这一点Global.asax
。这是一个很好的概述
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Map("Way1", "Agency/{name}/{IdAgency}/{Page}", MVC.Agency.Index().AddRouteValue("page", 1)
, new { Page = @"\d*" } );
routes.Map("Way2", "Agency/{name}", MVC.Agency.Index() );
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
这是我创建了几个与 T4MVC 一起使用的扩展
public static class RouteExtensions
{
#region Map
public static Route Map(this RouteCollection routes, string routename, string url,
ActionResult result)
{
return routes.Map(routename, url, result, null, null, null);
}
public static Route Map(this RouteCollection routes, string routename, string url,
ActionResult result, object constraints)
{
return routes.Map(routename, url, result, null, constraints, null);
}
public static Route Map(this RouteCollection routes, string routename, string url,
ActionResult result, object defaults, object constraints, string[] namespaces)
{
return routes.MapRoute(routename, url, result, defaults, constraints, namespaces)
.SetRouteName(routename);
}
#endregion
public static string GetRouteName(this RouteValueDictionary routeValues)
{
if (routeValues == null)
{
return null;
}
object routeName = null;
routeValues.TryGetValue("__RouteName", out routeName);
return routeName as string;
}
public static Route SetRouteName(this Route route, string routeName)
{
if (route == null)
{
throw new ArgumentNullException("route");
}
if (route.DataTokens == null)
{
route.DataTokens = new RouteValueDictionary();
}
route.DataTokens["__RouteName"] = routeName;
return route;
}
}