0

我想从我的 MVC 网站中的所有 URL 中删除控制器名称。我在 URL 中使用文化,但不会删除它。我有三个默认地图(在 URL 中带有控制器名称):文化,SEO 友好,默认

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

如果我从第一个和第二个路由图中删除 {controller} 和(或)三个所有链接都会出现问题。(404 错误,或不加载数据,或具有标题或 ID 的 URL 被破坏)。无论如何,我对此有很大的问题,不知道该怎么做。当然,我有设置文化和呼叫路线图等的基本控制器。 在此处输入图像描述

我测试了不同的方法,但没有解决我的问题。我也从这个问题中得到了帮助,但没有解决。从 URL 中删除控制器名称 解决我的问题的一种方法是使用这些路由,但是有很多例外,我必须为每个控制器定义多个路径,这没有响应,另一方面,当写作和重定向没有发生时,我的主要地址是可以访问的。

请帮我解决这个问题。我想要一个全面的方法来回答所有的路径。最后我的代码是:

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" }
        );
    }
4

0 回答 0