zoukankan      html  css  js  c++  java
  • MVC4 使用Areas后存在相同Controller时报错

    摘要:Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search

    报错如下:

    Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{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 'Home' has found the following matching controllers:
    MVC.Controllers.HomeController
    MVC .Areas.Search.Controllers.HomeController

    问题原因:使用Areas后存在多个相同的Controller,路由注册未分开

    解决方法:

    修改SearchAreaRegistration文件 RegisterArea 方法如下

    public override void RegisterArea(AreaRegistrationContext context)
    {
    context.MapRoute(
    "Search_default",
    "Search/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional },
    new string[] { "MVC.Areas.Search.Controllers" }
    );
    }

    修改RouteConfig文件内 RegisterRoutes 方法如下

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

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

  • 相关阅读:
    jdbc连接Mysql数据库
    测试ibatis3连接数据
    dbcp参数配置
    努力---是永远且持久的行为
    android---textview控件学习笔记之显示表情图片和文本(二)
    android---textview控件学习笔记之显示文本(一)
    程序员的要求
    android的adb命令中,pm,am的使用
    完成celery简单发送注册邮件
    培养代码逻辑
  • 原文地址:https://www.cnblogs.com/zcm123/p/3096058.html
Copyright © 2011-2022 走看看