Register ASP.NET MVC

I have few problems registering the "Admin" area, which is stored in AdminAreaRegistration.cs inside the "Admin" folder. My routes look like this:

        context.MapRoute(
            "Admin_News",
            "Admin/News",
            new { controller = "News", action = "Index" }
        );

        context.MapRoute(
            "Admin_Pages",
            "Admin/Pages",
            new { controller = "Pages", action = "Index" }
        );

        context.MapRoute(
            "Admin_Galleries",
            "Admin/Galleries",
            new { controller = "Galleries", action = "Index" }
        );

        context.MapRoute(
            "Admin_Categories",
            "Admin/Categories",
            new { controller = "Categories", action = "Index" }
        );

        context.MapRoute(
            "Admin_Products",
            "Admin/Products",
            new { controller = "Products", action = "Index" }
        );

        context.MapRoute(
            "Admin_Files",
            "Admin/Files",
            new { controller = "Files", action = "Index" }
        );

        context.MapRoute(
            "Admin_Orders",
            "Admin/Orders",
            new { controller = "Orders", action = "Index" }
        );

        context.MapRoute(
            "Admin_Codes",
            "Admin/Codes",
            new { controller = "Codes", action = "Index" }
        );

        context.MapRoute(
            "Admin_Login",
            "Admin/Login",
            new { controller = "Login", action = "Index" }
        );

        context.MapRoute(
            "Admin_Panel",
            "Admin",
            new { controller = "Index", action = "Index" }
        );

        context.MapRoute(
            "Admin_Default",
            "Admin/{controller}/{action}/{param}",
            new { param = UrlParameter.Optional }
        );

Problem: when I get the login in the message, I use

return Redirect("~/Admin");

Everything is fine until I want to click the link (which is well generated)

<li><a href="@Url.RouteUrl("Admin_Categories")">Categories</a></li> ==
<li><a href="~/Admin/Categories">Categories</a></li>

Then I should be redirected to the Index action for the category controller, but instead, I will be redirected to the index action of the input controller. Any ideas why this is happening like this?

Yours faithfully!

EDIT 1:

Well, I changed the Admin_Categories route to this:

context.MapRoute(
    "Admin_Categories",
    "Admin/Categories/{action}",
     new { controller = "Categories" }
);

This seems to work, but now the url generates an action name that I would not want in the case of an index action:

<li><a href="@Url.RouteUrl("Admin_Categories")">Categories</a></li> ==
<li><a href="~/Admin/Categories/Index">Categories</a></li>

How to remove action name from url?

EDIT 2

, - controller Index action view . . ( VarByParam , ):

[OutputCache(CacheProfile = "OneDayCache")]

. , . ... ?

3

AreaRoutes:

context.MapRoute(
    "Admin_Categories",
    "Admin/Categories",
    new { controller = "Categories", action = "Index", param = UrlParameter.Optional },
    new[] { "AMBIT_CMS_MVC.Areas.Admin.Controllers" }
);

context.MapRoute(
    "Admin_Default",
    "Admin/{controller}/{action}/{param}",
    new { controller = "Index", action = "Index", param = UrlParameter.Optional },
    new[] { "AMBIT_CMS_MVC.Areas.Admin.Controllers" }
    );

URL- :

@Url.RouteUrl("Admin_Categories")

, . - Global.asax, , ...

EDIT 4

, - . Chrome - /Admin/Categories /Admin/Login, , URL- url/Admin/Categories, /Admin/Login.

OutputCache, , .

[OutputCache(CacheProfile = "OneDayCache", VaryByCustom = "Url")]
public ActionResult Index(){...}

VaryByCustom Global.asax:

public override string GetVaryByCustomString(HttpContext context, string arg)
    {
        if (arg.Equals("Url"))
        {
            string url = !string.IsNullOrWhiteSpace(context.Request.Url.AbsoluteUri) ? context.Request.Url.AbsoluteUri : string.Empty;
            return "Url=" + url;
        }
        return base.GetVaryByCustomString(context, arg);
    }

http://localhost:50945/Admin/Categories
HTTP/1.1 302 Found
Cache-Control: private, max-age=86400
Content-Type: text/html; charset=utf-8
Expires: Thu, 09 Feb 2017 20:24:19 GMT
Last-Modified: Wed, 08 Feb 2017 20:24:19 GMT
Location: /Admin/Login
Server: Microsoft-IIS/10.0
X-AspNetMvc-Version: 5.2
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?TTpcUHJvamVrdHlcQU1CSVQtQ01TLU1WQ1xBTUJJVCBDTVMgTVZDXEFkbWluXENhdGVnb3JpZXM=?=
X-Powered-By: ASP.NET
Date: Wed, 08 Feb 2017 20:24:19 GMT
Content-Length: 439

, VaryByCustom . , ?

+4
4

:

. action.

, :

context.MapRoute(
    "Admin_Categories",
    "Admin/Categories/{action}",
     new { controller = "Categories", action = "Index" }
);

:

, :

context.MapRoute(
    "Admin_Default",
    "Admin/{controller}/{action}/{param}",
    new { param = UrlParameter.Optional }
);

, , /Admin/Foo/Bar :

  • FooController Admin -Area
  • Bar -Method .

URL

@Url.Action("ActionName", "ControllerName", new { Area = "AreaName" })

, :

context.MapRoute(
    "Admin_Default",
    "Admin/{controller}/{action}/{param}",
    new { controller = "Index", action = "Index", id = UrlParameter.Optional }
);

@Url.Action("Index", "ControllerName", new { Area = "AreaName" })

Url /AreaName/ControllerName /AreaName/ControllerName/Index.

@Url.Action("Index", "Index", new { Area = "AreaName" })

Url /AreaName /AreaName/Index/Index.

:

public class AdminAreaRegistration : AreaRegistration 
{
    public override string AreaName 
    {
        get 
        {
            return "Admin";
        }
    }

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

, . MapRoute.

, enogh , :

@Url.Action("SomeAction")

, , :

@Url.Action("SomeAction", "SomeController")
+5

?

[Area("Admin")]
[Route("Categories")]
public class CategoriesController : Controller

, , ASPNET Core, MVC ?

0

. , ? [Authorize] "" , .

0

Area MVC:

AdminAreaRegistration.cs:

context.MapRoute(
    "admin_default",
    "admin/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional }
);

:

[Area("Admin")]
public class MyController : Controller

global.asax, Application_Start() ( ):

AreaRegistration.RegisterAllAreas();

:

Url.Action("Index", "Categories", new { Area = "Admin" })

. !

0

Source: https://habr.com/ru/post/1667905/


All Articles