C# NETMVC区域注册
我的“管理员”区域注册没有什么问题,它存储在“管理员”区域文件夹中的adminaregistration.cs中。我的路线如下所示:C# NETMVC区域注册,c#,asp.net,asp.net-mvc,routes,area,C#,Asp.net,Asp.net Mvc,Routes,Area,我的“管理员”区域注册没有什么问题,它存储在“管理员”区域文件夹中的adminaregistration.cs中。我的路线如下所示: context.MapRoute( "Admin_News", "Admin/News", new { controller = "News", action = "Index" } ); context.MapRoute(
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 }
);
问题是:当我登录帖子时,我使用
return Redirect("~/Admin");
一切都很好,直到我想点击链接(生成良好)
这似乎是可行的,但现在url是用操作名生成的,在索引操作的情况下,我不会这样做:
<li><a href="@Url.RouteUrl("Admin_Categories")">Categories</a></li> ==
<li><a href="~/Admin/Categories/Index">Categories</a></li>
但即使清除缓存也无法修复问题。我仍然被重定向到登录控制器索引操作。。。有什么想法吗
编辑3
我使用了这些路线:
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中注册,没有任何路由可以覆盖这些路由
编辑4
我发现我的问题是缓存。Chrome以某种方式将我的/Admin/Categories位置缓存为/Admin/Login,因此当我点击url/Admin/Categories时,缓存会将其重定向到/Admin/Login
我使用OutputCache属性,但它似乎不能正常工作
[OutputCache(CacheProfile = "OneDayCache", VaryByCustom = "Url")]
public ActionResult Index(){...}
在Global.asax中设置VaryByCustom:
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
也许这辆车坏了。有没有办法解决这个问题?你有没有尝试过用路由属性装饰你的控制器类
[Area("Admin")]
[Route("Categories")]
public class CategoriesController : Controller
这在我遇到的一个类似问题上对我起了作用,我使用的是ASPNET Core,您使用的是什么版本的MVC?您的路由代码对我来说很好。您确定在登录方法中正确验证了您的身份吗?只需尝试从“类别”控制器中删除[Authorize]标记,看看是否存在授权问题 到您的第一次编辑: 第三个参数注册默认值。您缺少默认的
操作
将您的路线更改为此,它将工作:
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
得到如下解析:
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 }
);
- 在
-区域中查找Admin
FooController
- 调用条上的
方法
@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.Action("Index", "Index", new { Area = "AreaName" })
生成的Url将是/AreaName/ControllerName
,而不是/AreaName/ControllerName/Index
如果你打电话
@Url.Action("Index", "ControllerName", new { Area = "AreaName" })
@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
的第三个参数中设置默认值
另外请注意,如果您已经在同一控制器的视图中,则可以编写操作:
@Url.Action("SomeAction")
如果您在同一区域的视图中,但想调用另一个控制器,您可以这样写:
@Url.Action("SomeAction", "SomeController")
这是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\u Start()
方法上添加以下代码行(最重要的是):
鉴于使用:
Url.Action("Index", "Categories", new { Area = "Admin" })
这种方法应该很管用。测试 不工作。仍在重定向到登录控制器索引操作。这可能与您定义路由的方式有关,因为我正在使用ASPNET核心应用程序。mine的定义如下..app.UseMvc(routes=>{routes.MapRoute(“areaRoute”,“{area:exists}/{controller=Admin}/{action=Index}/{id?}”);routes.MapRoute(名称:“default”,模板:{controller=Home}/{action=Index}/{id?}”);};我有自己的授权系统,已经调试过,运行良好。
new{controller=“Categories”}
指定了默认值,但是您没有{action}
的默认值,所以将action=“Index”
添加到最后一个MapRoute语句默认值中。在这种情况下,路由将我重定向到登录控制器索引操作。但是链接的url是~/Admin/Categories。最有可能的是,在您在此发布的路由配置中存在的路由之前配置的某些路由在/Admin/Categories
上匹配。但是,除非您在此处发布整个路由配置,包括其他区域、属性路由和RouteConfig.cs
文件中的路由,否则没有人能告诉您出了什么问题。请确保包括调用AreaRegistration.Registeralareas()
和RouteConfig.RegisterRoutes()
(特别是它们的调用顺序)的行。好吧,我是按照您编写的那样做的,但不起作用。仍将重定向到登录索引而不是类别索引。是否确实使用了AreaAttribute
?我发现在MVC5中,在控制器类级别定义区域应该是RouteAreaAttribute
。您好,谢谢您的回答,但它仍然不起作用。奇怪的是,当我使用路径localhost/Admin/Categories时,我被重定向到localhost/Admin/Login。但当我使用路径localhost/Admin/Categories/Index时,我就可以使用Categories控制器的正确索引操作了,它工作得很好。但我不想使用../Index来执行控制器索引操作。它与缓存或登录系统无关。我确信问题出在路由内部。。。有什么想法吗?你现在做了什么?我的回应来自你的编辑1还是更好的方式@ashiv3r查看编辑3以查看我的更改。我试图进入同一区域内的视图。如果你被重定向(HTTP 301等),这与路由无关。很可能是通过授权。也请重读我的答案。我为您提供了编辑1的解决方案,以及另一个更好的解决方案。您的新编辑似乎忽略了这两个方面@Ashiv3rWell,我评论了编辑3中的第一条路线,只留下了最后一条(与您的相同)a
Url.Action("Index", "Categories", new { Area = "Admin" })