2012-05-10 42 views
-1

1)什么是与T4MVC工作的最佳解决方案,已经产生格式化的URL(SEO)T4MVC已生成格式的URL(SEO)

我想MVC.AGENCY.INDEX (int? Page, int IdAgency)

http://localhost:30120/Agency/AgencyName 

代替

http://localhost:30120/Agency?page=0&IdAgence=2 

我能有这样

http://localhost:30120/Agency?page=0&IdAgency=2&agency=agencyName 

与AddMaperoute()但我不想(Agency?page=0&IdAgency=2)在URL中

也许更改符号&和= by /?

2)当我添加

我用 http://blog.ashmind.com/2010/03/15/multiple-submit-buttons-with-asp-net-mvc-final-solution/

<input type="submit" name=="Agency" value="" class="button bbrightRed mr25" /> 


public virtual ActionResult Agency (AgencyViewModel _AgencyViewModel) 
{ 
.... 
View return (_AgencyViewModel). AddRouteValue ("AgencyName", AgencyName); 
} 

我想补充一些信息URL

我有一个exeption当我添加View return (_AgencyViewModel). AddRouteValue ("AgencyName", AgencyName); 错误地称为T4MVC WAS。您可能需要电源通过右击再生它将T4MVC.tt并选择运行自定义工具

我不AddRouteValue()URL是http://localhost:30120/Agency 我想

http://localhost:30120/Agency/Agancyname/fff-llll-mm 

回答

0

如果你不需要page=0&IdAgency=2至少有2种选择:

  1. 与网址替换它像http://localhost:30120/Agency/AgencyName/2/0和使用MVC.AGENCY.INDEX (string name, int? Page, int IdAgency)(见WAY1以下路由)

  2. 从控制器中删除标识和页面,并仅按名称映射(仅当它是唯一的时)。你必须http://localhost:30120/Agency/AgencyName和使用MVC.AGENCY.INDEX (string name)(见Way2以下路由)

有你需要注册路线的搜索引擎优化的URL。您可以在Global.asaxApplication_Start方法中做到这一点。 Here is a good overview

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; 
} 

} 
+0

谢谢你,但VS认不出我setRouteName()? – chadis

+0

@chadis谢谢。我已将Get/SetRouteName方法添加到答案中。 – Cheburek