簡體   English   中英

在ASP.NET MVC中使用區域時如何配置路由?

[英]How to configure routes when using areas in asp.net mvc?

場景:我有3個區域,分別是AlbumsSingersMusic,現在每個區域都有名稱相同的控制器。 例如,每個區域都有LoginController。

現在我正在關注以下異常

找到多個與名為“登錄”的控制器匹配的類型。如果為該請求提供服務的路由未指定名稱空間來搜索與該請求匹配的控制器,則會發生這種情況。 如果是這種情況,請通過調用帶有“名稱空間”參數的“ MapRoute”方法的重載來注冊此路由。

這是Visual Studio在創建區域時自動生成的配置

 public override string AreaName 
    {
        get 
        {
            return "Albums"
        }
    }

    public override void RegisterArea(AreaRegistrationContext context) 
    {
        context.MapRoute(
            "Albums_Default"
            "Client/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }

這是我在RoutesConfig.cs中的初始配置

  routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "Application_Name" }
        );

現在,如何配置路由,而無需在url中進行任何修改即可呈現所需的視圖。

請試試這個 :

  public static void RegisterRoutes(RouteCollection routes)
    {
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

      routes.MapRoute(
        "Default",                                              // Route name
        "{controller}/{action}/{id}",                           // URL
        new { controller = "Home", action = "Index", id = "" }, // Defaults
        new[]{"AreasDemoWeb.Controllers"}                       // Namespaces
      );
    }

幫助鏈接1

幫助鏈接2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM