简体   繁体   English

MVC 3区域注册302错误

[英]MVC 3 Area Registration 302 Error

I have a 2 area which names called with "Admin" and "Services". 我有2个区域,名称分别为“ Admin”和“ Services”。 My project structure as below image 1 我的项目结构如下图1所示

I want to redirect all root url to Admin area. 我想将所有根URL重定向到Admin区域。 I changed my area registration files to resolve this problem as below code block. 我更改了我的区域注册文件以解决此问题,如下代码块所示。

//Root 
//Old Value
routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
//New Value
routes.MapRoute(
                "Default", // Route name
                "trash/{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );

Now for redirecting the root url to Admin area I've updated AdminAreaRegistration.cs class as below. 现在,为了将根URL重定向到Admin区域,我更新了AdminAreaRegistration.cs类,如下所示。

//Admin
//Old Value
context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                 new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

//New Value
context.MapRoute(
                "Admin_default",
                "{controller}/{action}/{id}",
                 new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

After this update when i want to browse my web site it was ok I can see my admin area views. 更新之后,当我想浏览我的网站时,可以看到我的管理区视图。 But I have a lot of AJAX call on Admin Area Views and when those ajax calls try to call Services Area controller it could not access to the Services Controller, I've monitored on network plugin to see what is my error I get a HTTP 302 error and then its redirect to 404 Not Found page. 但是我在“管理区域视图”上有很多AJAX调用,当这些ajax调用尝试调用服务区域控制器时,它无法访问服务控制器,我已经在网络插件上进行了监视,以查看我收到的HTTP 302错误是什么错误,然后将其重定向到404 Not Found页面。

I hope my explanation is understable. 我希望我的解释是不稳定的。

Thank you in advance. 先感谢您。

项目结构

If you add below parameters to your MapRoute it will be fixed. 如果将以下参数添加到MapRoute,它将得到修复。

.DataTokens.Add("area", "Admin"); 

And

 namespaces: new[] { "X.Areas.Admin.Controllers" } ).

Final Output 最终输出

routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new {area="Admin",  controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "X.Areas.Admin.Controllers" }
                ).DataTokens.Add("area", "Admin");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM