ASP.NET MVC 区域未选择正确的路线
我正在调试 MVC 3 应用程序上的路由问题,并且正在使用 Phil Hacks 路由调试器。
我似乎无法弄清楚下面以黄色突出显示的路线的起点。每次我使用以下请求运行应用程序
http://www.mywebsite.com/ auth/login?ReturnUrl=/
这条路由首先出现,然后给我一个 404 错误,因为我没有索引操作。如您所见,我已将默认路由设置为使用登录操作方法,但该路由仍然存在。
我有以下路由配置:
AuthAreaRegistration
public class AuthAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Auth";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"login",
"auth/login/{*returnPath}",
new { controller = "Auth", action = "LogIn", id = UrlParameter.Optional }
);
context.MapRoute(
"Auth_default",
"Auth/{controller}/{action}/{id}",
new { controller = "Auth", action = "LogIn", id = "" }
);
}
}
Global.asax(使用 T4 MVC 模板)
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Home",
"{controller}/{action}/{id}",
MVC.Home.Index(), new { id = UrlParameter.Optional },
new string[] { "MyNamespace.WebUI.Controllers" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
MVC.Home.Index(), new { id = UrlParameter.Optional },
new string[] { "MyNamespace.WebUI.Controllers" }
);
}
I am in the process of debugging a routing issue on my MVC 3 application and I am using Phil Hacks routing debugger.
I cannot seem to work out where the route highlighted in yellow below is originating. Each time I run my application with the following request
http://www.mywebsite.com/auth/login?ReturnUrl=/
this route comes first and then gives me a 404 error as I do not have an index action. As you can see I have set my default routes to use the Login action method but still this route persists.
I have the following route configurations:
AuthAreaRegistration
public class AuthAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Auth";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"login",
"auth/login/{*returnPath}",
new { controller = "Auth", action = "LogIn", id = UrlParameter.Optional }
);
context.MapRoute(
"Auth_default",
"Auth/{controller}/{action}/{id}",
new { controller = "Auth", action = "LogIn", id = "" }
);
}
}
Global.asax (Using T4 MVC Templates)
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Home",
"{controller}/{action}/{id}",
MVC.Home.Index(), new { id = UrlParameter.Optional },
new string[] { "MyNamespace.WebUI.Controllers" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
MVC.Home.Index(), new { id = UrlParameter.Optional },
new string[] { "MyNamespace.WebUI.Controllers" }
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不想回答我自己的问题,但经过一天的尝试解决这个问题后,我想我会发布答案,以防其他人遇到同样的问题。
事实证明,我的应用程序保留了旧路线并将它们填充到我的路线集合中。我删除了 bin 文件夹中的所有文件并重建了我的解决方案,一切正常。
我在这里更详细地回答了这个问题:
Does ASP .NET MVC为区域创建默认路由
I don’t like to answer my own question but after a day of trying to solve this problem I thought I would post the answer in case anyone else has the same issue.
It turns out that my application was holding onto old routes and populating them into my route collection. I deleted all the files in my bin folder and rebuilt my solution and everything worked as it should.
I have answered this question in a little more detail here:
Does ASP.NET MVC create default routes for areas
我认为问题在于,您在外部有一个名为 Auth 的区域和一个名为 Auth 的控制器的地区。
MVC 会首先尝试将您的 url 与身份验证区域进行匹配,但您实际上希望它到达区域之外的身份验证控制器。
解决这个问题的最好方法是避免控制器/区域的名称不明确。
I think the problem is in the fact that you have an area called Auth and a controller called Auth outside of the areas.
MVC will try to match your url against Auth area first but you actually want it to hit your auth controller outside an area.
The best way to solve it imho is to avoid a ambiguous names of controllers/areas.