Bereichsordner sehen folgendermaßen aus:
Areas
Admin
Controllers
UserController
BranchController
AdminHomeController
Projektverzeichnisse sehen wie folgt aus:
Controller
UserController
GetAllUsers
gebietsroutenregistrierung
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new { controller = "Branch|AdminHome|User" }
);
}
projektroutenregistrierung
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 string[] { "MyApp.Areas.Admin.Controllers" });
}
Wenn ich wie folgt route: http://mydomain.com/User/GetAllUsers
Ich erhalte den Fehler Ressource nicht gefunden (404). Ich erhalte diesen Fehler, nachdem ich UserController zu Area hinzugefügt habe.
Wie kann ich diesen Fehler beheben?
Vielen Dank...
Sie haben Ihre Controller-Namespaces durcheinander gebracht.
Ihre Hauptroutendefinition sollte sein:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "MyApp.Controllers" }
);
Die Registrierung für den Admin-Bereich sollte folgendermaßen lauten:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new { controller = "Branch|AdminHome|User" },
new[] { "MyApp.Areas.Admin.Controllers" }
);
}
Beachten Sie, wie die richtigen Namespaces verwendet werden sollen.
Eine aktuelle Lösung für ASP.NET Core MVC.
[Area("Products")]
public class HomeController : Controller
Quelle: https://docs.Microsoft.com/en-us/aspnet/core/mvc/controllers/areas