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:
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 . , ?