-1

1) 使用 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 中使用( )

也许将符号 & 和 = 更改为 /?

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

添加时我有一个例外View return (_AgencyViewModel). AddRouteValue ("AgencyName", AgencyName);错误地称为 T4MVC WAS。您可能需要通过右键单击 T4MVC.tt 并选择运行自定义工具来重新生成它

我没有 AddRouteValue() 的 URL 是http://localhost:30120/Agency 我想要

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

1 回答 1

0

如果您不需要page=0&IdAgency=2,至少有 2 个选项:

  1. http://localhost:30120/Agency/AgencyName/2/0用 url like和 using替换它 MVC.AGENCY.INDEX (string name, int? Page, int IdAgency)(参见下面路由中的 Way1)

  2. 完全从控制器中删除 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;
}

}
于 2012-05-17T09:20:19.960 回答