MVC3自定义路由无法正常工作 [英] MVC3 Custom Routing doesnt work properly

查看:85
本文介绍了MVC3自定义路由无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,,,,

我对asp.net mvc 3路由有一个疑问..我在mvc3做一个项目。在我的应用程序中有一个管理员端和公共用户端..

我有一个控制器是adminController来登录管理员..在adminController中有两个用于登录和home的动作方法。所以网址看起来像

www .mySite.com / admin / login



www.mySite.com/admin/home



I在控制器文件夹中创建了另一个文件夹Admin,仅用于访问管理员并在Admin文件夹中创建ManageMembersController ...并且URL应该看起来像



www.mySite.com/ admin / ManageMembers / list



我已在全局asax文件中编写自定义路由..

路由。 MapRoute(
Admin,//路由名称
Admin / {controller} / {action} / {data},//带参数的URL

new
{
controller =/ Admin / ManageMembers,
action =list,
data = UrlParameter.Optional
} //参数默认值
);
routes.MapRoute(
admin_default,//路由名称
admin / {action} / {id},//带参数的URL
new {controller = admin,action =login,id = UrlParameter.Optional} //参数默认为
);
routes.MapRoute(
Default,//路由名称
{controller} / {action} / {id},//带参数的URL
new {controller =Pub,action =Index,id = UrlParameter.Optional} //参数默认为
);









我的问题是当我试图访问www.mySite.com/admin/login时出现错误

'资源不能是发现。'

但其他路由工作正常..

即www.mySite.com/admin/ManageMembers/list重定向到ManageMembersController



当网址为www.mysite.com/admin/时,我的要求是重定向到Controller / adminController.cs。

并且必须重定向到Controller /Admin/ManageMembers.cs当网址是www.mySite.com/Admin/ManageMembers/ ..

我需要你的建议...





谢谢Devguys ...

解决方案

要使两个不同的控制器具有相同的名称,您可能需要将它们组织成单独的区域 [ ^ ]。据我所知,您将能够为路由中的每个控制器指定相应的区域,如下所示:



 routes.MapRoute(
默认
{area} / {controller} / {action} / {id}
new {area = YourArea,controller = Admin,action = 列表,id = UrlParameter.Optional}
);





等...



我希望这有用。

Phil


如果你运行怎么办有多个子文件夹?

Ex:Admin / Component / ComUser / User /

Admin / Component / ComMenu / Menu /

hi all,,,,
I have one doubt on asp.net mvc 3 routing..Iam doing a project in mvc3.In my application there is an admin side and also public user side..
I have a controller that is adminController to login the admin..In the adminController there is two action methods for login and home.So the url look like
www.mySite.com/admin/login

www.mySite.com/admin/home

I have created another folder Admin in the controller folder only for accessing administrators and created ManageMembersController in the Admin folder..and the url should look like

www.mySite.com/admin/ManageMembers/list

I have written the custom routing in the global asax file..

routes.MapRoute(
                           "Admin", // Route name
                           "Admin/{controller}/{action}/{data}", // URL with parameters

                           new
                           {
                               controller = "/Admin/ManageMembers",
                               action = "list",
                               data = UrlParameter.Optional
                           } // Parameter defaults
                       );
            routes.MapRoute(
               "admin_default", // Route name
               "admin/{action}/{id}", // URL with parameters
               new { controller = "admin", action = "login", id = UrlParameter.Optional } // Parameter defaults
           );
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Pub", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );





My problem is that when iam trying to www.mySite.com/admin/login I got an error
'The resource cannot be found.'
but the other routing worked properly..
that is www.mySite.com/admin/ManageMembers/list it redirected to the ManageMembersController

My requirement is to redirect to Controller/adminController.cs when the url is www.mysite.com/admin/..
and it must redirect to Controller/Admin/ManageMembers.cs when the url is www.mySite.com/Admin/ManageMembers/..
I need your suggestions...


Thank you Devguys...

解决方案

To have two different controllers with the same name, you'll probably need to organize them into separate areas[^]. As far as I know, you'll then be able to specify the corresponding area for each controller in the routing with something like this:

routes.MapRoute(
     "Default",
     "{area}/{controller}/{action}/{id}",
     new { area = "YourArea", controller = "Admin", action = "List", id = UrlParameter.Optional }
);



etc...

I hope this is helpful.
Phil


how to run if you have multi subfolder?
Ex: Admin/Component/ComUser/User/
Admin/Component/ComMenu/Menu/


这篇关于MVC3自定义路由无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆