I have little problem with my "Admin" area registration, which are stored in AdminAreaRegistration.cs inside "Admin" area 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 is: When I get login on post I use
return Redirect("~/Admin");
Everything is fine, until I want to click on link (which is generating well)
<li><a href="@Url.RouteUrl("Admin_Categories")">Categories</a></li> ==
<li><a href="~/Admin/Categories">Categories</a></li>
Then I'm suppose to be redirected to Index action of Categories controller, but instead I'm redirected to Index action of Login controller. Any ideas why is it happening like this?
Best regards!
EDIT 1:
Well, I've changed Admin_Categories route to this one:
context.MapRoute(
"Admin_Categories",
"Admin/Categories/{action}",
new { controller = "Categories" }
);
It seems to work, but now url is generating with action name, which I wouldn't be like this in case of Index action:
<li><a href="@Url.RouteUrl("Admin_Categories")">Categories</a></li> ==
<li><a href="~/Admin/Categories/Index">Categories</a></li>
How can I remove action name from url?
EDIT 2
Okay, so somehow my browser cached Categories controller Index action view as Login controller Index action view. Problem was solved for a while after clearing browser cache. I used below cache attribute for all views (just used VarByParam for views that need parameters):
[OutputCache(CacheProfile = "OneDayCache")]
But even clearing cache doesn't repair the problem. Still I'm getting redirected to Login controller Index action... Any ideas?
EDIT 3
I used those 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" }
);
I used this to create route url:
@Url.RouteUrl("Admin_Categories")
But still not working as it should. Admin routes are registered in Global.asax and there isn't any route that can override those...
EDIT 4
I've found that my problem is caching. Chrome somehow cache my /Admin/Categories location as /Admin/Login, so when i hit url /Admin/Categories cache is redirecting it to /Admin/Login.
I use OutputCache attribute, but it seems not work properly.
[OutputCache(CacheProfile = "OneDayCache", VaryByCustom = "Url")]
public ActionResult Index(){...}
VaryByCustom set in 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
Maybe this VaryByCustom isn't working. Any ideas how to fix it?
new { controller = "Categories" }
specifies the defaults, but you have no default for{action}
, so addaction = "Index"
to the last MapRoute statement defaults. – Brian Mains/Admin/Categories
. But nobody can tell you what is wrong unless you post your entire route configuration here, including other areas, attribute routes, and routes from yourRouteConfig.cs
file. Be sure to include the lines where you make the calls toAreaRegistration.RegisterAllAreas()
andRouteConfig.RegisterRoutes()
(specifically what order they are called in). – NightOwl888