I want remove controller name form all URLs in my MVC web site. I Use culture in URL but won't delete this. I have three map for default(with controller name in URL):for culture ,SEO friendly, default
routes.MapRoute(
name: "SeoFriendly",
url: "{culture}/{controller}/{action}/{id}/{title}",
defaults: new { culture = "fa", controller = "home", action = "index", title = UrlParameter.Optional },
constraints: new { culture = "[a-z]{2}", id = @"\d+" },
namespaces: new[] { "WebSite.Controllers" }
);
routes.MapRoute(
name: "WithCulture",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { culture = "fa", controller = "home", action = "index", id = UrlParameter.Optional },
constraints: new { culture = "[a-z]{2}" },
namespaces: new[] { "WebSite.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "home", action = "index", id = UrlParameter.Optional },
namespaces: new[] { "WebSite.Controllers" }
);
if I remove {controller} from first and second route map and(or) three all Links become problematic.(404 error ,or don't load data ,or URL that has title or id is spoiled).
anyway I have very problems by this and don't know what do.
Of course I have base controller that set culture and call route map and etc.
I tested different ways but don't resolve my problem. Also I got help from this question but don't resolve.Removing controller name from URL One of the ways that solved my problem a bit was to use these routes, but there were many exceptions, and I had to define several paths for each controller, and this was not responsive, and on the other hand, my main addresses were accessible when writing and redirects did not occur.
please help me for resolve this problem. I want a comprehensive way to answer all the paths. last my code this is:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// routes.IgnoreRoute("elfinder.connector");
routes.LowercaseUrls = true;
routes.MapRoute(
name: "elmah",
url: "elmah/{type}",
defaults: new { controller = "elmah", action = "details", type = UrlParameter.Optional }
).DataTokens = new RouteValueDictionary(new { area = "manager" });
routes.MapRoute(
name: "home2",
url: "{culture}/services/{id}/{title}",
defaults: new { culture = "fa", controller = "home", action = "services", title = UrlParameter.Optional },
constraints: new { culture = "[a-z]{2}", id = @"\d+" },
namespaces: new[] { "WebSite.Controllers" }
);
routes.MapRoute(
name: "cart",
url: "{culture}/cart/{action}",
defaults: new { culture = "fa", controller = "cart", action = "index" },
constraints: new { culture = "[a-z]{2}" },
namespaces: new[] { "WebSite.Controllers" }
);
routes.MapRoute(
name: "home",
url: "{culture}/{action}",
defaults: new { culture = "fa", controller = "home", action = "index" },
constraints: new { culture = "[a-z]{2}" },
namespaces: new[] { "WebSite.Controllers" }
);
routes.MapRoute(
name: "carpet",
url: "{culture}/{action}/{id}/{title}",
defaults: new { culture = "fa", controller = "carpet", action = "details", title = UrlParameter.Optional },
constraints: new { culture = "[a-z]{2}", id = @"\d+" },
namespaces: new[] { "WebSite.Controllers" }
);
routes.MapRoute(
name: "SeoFriendly",
url: "{culture}/{controller}/{action}/{id}/{title}",
defaults: new { culture = "fa", controller = "home", action = "index", title = UrlParameter.Optional },
constraints: new { culture = "[a-z]{2}", id = @"\d+" },
namespaces: new[] { "WebSite.Controllers" }
);
routes.MapRoute(
name: "WithCulture",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { culture = "fa", controller = "home", action = "index", id = UrlParameter.Optional },
constraints: new { culture = "[a-z]{2}" },
namespaces: new[] { "WebSite.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "home", action = "index", id = UrlParameter.Optional },
namespaces: new[] { "WebSite.Controllers" }
);
}