내 프로젝트에서 같은 이름의 여러 컨트롤러에 문제가 있습니다.
내 ASP.NET MVC 3 프로젝트에서 다음 오류가 발생합니다.
'Home'이라는 컨트롤러와 일치하는 여러 유형이 발견되었습니다. 이 요청을 서비스하는 경로 ( 'Home / {action} / {id}')가 요청과 일치하는 컨트롤러를 검색하기위한 네임 스페이스를 지정하지 않는 경우에 발생할 수 있습니다. 이 경우 'namespaces'매개 변수를 사용하는 'MapRoute'메소드의 오버로드를 호출하여이 경로를 등록하십시오.
'Home'에 대한 요청에서 일치하는 컨트롤러 MyCompany.MyProject.WebMvc.Controllers.HomeController MyCompany.MyProject.WebMvc.Areas.Company.Controllers.HomeController를 찾았습니다.
기본 컨트롤러 폴더에 클래스 이름이 MyCompany.MyProject.WebMvc.Controllers.HomeController 인 HomeController가 있습니다.
내 global.asax의 RegisterRoutes 메서드는 다음과 같습니다.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
그런 다음 Company라는 영역이 있고 해당 영역의 기본 컨트롤러 폴더에 HomeController가 있고 클래스 이름은 MyCompany.MyProject.WebMvc.Areas.Company.Controllers.HomeController입니다.
CompanyAreaRegistration 파일의 RegisterArea 메서드는 다음과 같습니다.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Company_default",
"Company/{controller}/{action}/{id}",
new { area = "Company", action = "Index", id = UrlParameter.Optional }
);
}
이 모든 것이이 게시물의 시작 부분에서 강조한 오류의 원인입니다. 나는 운이없는 다양한 다른 게시물의 솔루션을 모 으려고 고군분투하고 있습니다.
기본 컨트롤러 폴더에 HomeController가 있고 각 영역에 하나가있을 수 있습니까? 그렇다면이 작업을 수행하려면 내 구성 파일을 변경해야합니까?
어떤 도움이라도 대단히 감사하겠습니다!
오류 메시지에는 권장 솔루션이 포함되어 있습니다. "이 경우 'namespaces'매개 변수를 사용하는 'MapRoute'메소드의 오버로드를 호출하여이 경로를 등록하십시오."
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "MyCompany.MyProject.WebMvc.Controllers"}
);
이렇게하면 http : // server / 가 HomeController의 인덱스 작업으로 이동합니다. http : // server / company / home 은 영역 등록에 정의 된대로 회사 영역의 HomeController의 인덱스 작업으로 이동합니다.
이것은 asp.net mvc4 접근 방식입니다.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "RegisterNow", id = UrlParameter.Optional },
namespaces: new[] { "YourCompany.Controllers" }
);
네임 스페이스의 이름을 변경 했으므로 bin 및 obj 폴더 만 삭제 하고 다시 빌드 하고 다시 작업합니다.
이 문제의 또 다른 그럴듯한 원인은 다음과 같습니다.
'Home'이라는 컨트롤러와 일치하는 여러 유형이 발견되었습니다.
이것을 사용하십시오
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "ProjectName.Controllers" }
);
프로젝트 이름 만 사용하십시오.
Public Class RouteConfig
Public Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
routes.MapRoute( _
name:="Default", _
url:="{controller}/{action}/{id}", _
defaults:=New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _
, namespaces:={"MvcAreas"})
End Sub
RazorGenerator를 사용하는 경우 namespaces
매개 변수를 알리는 것만 으로는 충분하지 않을 수 있습니다.
아래에 표시된 진술을 추가하여 해결해야합니다 Global.asax.cs
.
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
ControllerBuilder.Current.DefaultNamespaces.Add("MyProject.Controllers"); // This one
}
Chris Moschini가 언급했듯이 네임 스페이스가 다른 동일한 컨트롤러 이름을 가진 두 영역이 있고 기본 없음 영역 경로가 500 서버 오류를 반환하는 경우 네임 스페이스 매개 변수가 충분하지 않을 수 있습니다.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "MyCompany.MyProject.WebMvc.Controllers"}
);
기본 경로 처리기를 재정의하고 다음 줄을 추가하는 것이 "최적"입니다.
RequestContext.RouteData.DataTokens["UseNamespaceFallback"] = false;
동일한 경로를 가진 다른 프로젝트에 대한 참조를 추가 한 후이 문제가 발생했으며 참조를 제거한 후에도 문제가 계속되었습니다.
bin 폴더에서 추가 된 참조의 .dll 파일을 삭제하고 다시 빌드하여 문제를 해결했습니다.
Like many others, I had this problem after creating a new MVC template project from VS2017 menu, on building the project I would get the op's error message. I then used the answer https://stackoverflow.com/a/15651619/2417292 posted earlier in this thread by cooloverride for mvc4 projects. This still didn't fix my issue so I renamed my Home view folder and HomeController file to be the view folder Company/ and controller file CompanyController. This then worked for me, not a fix per say but a workaround if your not stuck on having the route Home/Index my other issue was I couldn't find the reference causing the error and due to my dev platform being Azure WebApplication vs a full VM with a complete file system and IIS settings to mess with.
'developer tip' 카테고리의 다른 글
인덱스 된 모나드는 무엇입니까? (0) | 2020.08.28 |
---|---|
Android Studio가 앱에 변경 사항을 배포하지 않음 (0) | 2020.08.28 |
ManyToMany 관계에서 Django 제거 개체 (0) | 2020.08.28 |
오른쪽 대신 왼쪽으로 오버플로 (0) | 2020.08.28 |
웹 MVC 애플리케이션에서 액세스 제어 목록을 어떻게 구현할 수 있습니까? (0) | 2020.08.28 |