簡體   English   中英

C#MVC 5多個路由不工作的服務器

[英]C# MVC 5 Multiple routes not working server

我有一個項目,我試圖使地圖“簡短”讓網址看起來很漂亮。

在我的環境中,它正在工作,但是當發布到服務器時,它會給出以下錯誤。

我的網站:www.papodealemao.com.br

我的路線

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

    routes.MapRoute(
        name: "Pagina",
        url: "pagina/{id}",
        defaults: new { controller = "Principal", action = "Index", id = "1" }
    );

    routes.MapRoute(
        name: "Secao",
        url: "Secao/{id}",
        defaults: new { controller = "Blog", action = "Secao" }
    );

    routes.MapRoute(
        name: "Categoria",
        url: "Categoria/{id}",
        defaults: new { controller = "Blog", action = "Categoria" }
    );

    routes.MapRoute(
        name: "PorData",
        url: "PorData/{id}",
        defaults: new { controller = "Blog", action = "PorData" }
    );

    routes.MapRoute(
        name: "Artigo",
        url: "artigo/{id}",
        defaults: new { controller = "Blog", action = "Artigo" }
    );

    routes.MapRoute(
        name: "Tag",
        url: "Tag/{id}",
        defaults: new { controller = "Blog", action = "Tag" }
    );

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

錯誤

Multiple types were found that match the controller named 'Blog'. This can happen if the route that services this request ('artigo/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.

The request for 'Blog' has found the following matching controllers:
PapoDeAlemao.Controllers.BlogController
blog.Controllers.BlogController

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: Multiple types were found that match the controller named 'Blog'. This can happen if the route that services this request ('artigo/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.

The request for 'Blog' has found the following matching controllers:
PapoDeAlemao.Controllers.BlogController
blog.Controllers.BlogController

視覺工作室

在此輸入圖像描述

您需要在路由中添加名稱空間,以指示您想要的BlogController ,即:

routes.MapRoute(
    name: "Secao",
    url: "Secao/{id}",
    defaults: new { controller = "Blog", action = "Secao" },
    namespaces: new[] { "PapoDeAlemao.Controllers" }
);

出現錯誤的原因是,在“Controllers”文件夾中有一個名為“ BlogController ”的類,而在“PapoDeAlemao / Controllers”文件夾中有一個類。 請統一然后在一個班級,錯誤將消失。

要么

您可以命名空間路由

routes.MapRoute(
    name: "Secao",
    url: "Secao/{id}",
    defaults: new { controller = "Blog", action = "Secao" },
    namespaces: new[] { "PapoDeAlemao.Controllers" }
);

該錯誤消息表明您有多個名為BlogController類,並且路由表不知道您要路由到哪個類。 可能是你要刪除的雜散代碼。 也可能是仍在引用舊代碼的服務器上留下的舊dll。

嘗試清除以前的文件並重新發布。

暫無
暫無

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

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