-1

1) What is the best solution for working with T4MVC to have generated formatted URL (SEO)

I want to MVC.AGENCY.INDEX (int? Page, int IdAgency)

   http://localhost:30120/Agency/AgencyName

instead

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

I can have this

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

with AddMaperoute() but I don't want (Agency?page=0&IdAgency=2) in the URL

maybe change the symbols & and = by /?

2) When I add

I use 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);
}

I want add some information URL

I have an exeption when i add View return (_AgencyViewModel). AddRouteValue ("AgencyName", AgencyName); Incorrectly Called T4MVC WAS. You may Need to power to regenerate it by right clicking will be T4MVC.tt and Choosing Run Custom Tool

My URL without AddRouteValue() ishttp://localhost:30120/Agency And I want

http://localhost:30120/Agency/Agancyname/fff-llll-mm
chadis
  • 11
  • 1
  • 4

1 Answers1

0

If you don't need page=0&IdAgency=2 you have at least 2 options:

  1. replace it with url like http://localhost:30120/Agency/AgencyName/2/0 and using MVC.AGENCY.INDEX (string name, int? Page, int IdAgency) (see Way1 in routing below)

  2. remove id and page from the controller at all and map only by name (only when it is unique). You'll have http://localhost:30120/Agency/AgencyName and using MVC.AGENCY.INDEX (string name) (see Way2 in routing below)

To have seo urls you need to register routes. You can do that in Application_Start method in Global.asax. 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);
    }
}

Here is I have created several extensions to be used with 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;
}

}
Cheburek
  • 2,103
  • 21
  • 32