命名空间相当于ASP.NET MVC? [英] Namespaces equivalent in ASP.NET MVC?

查看:96
本文介绍了命名空间相当于ASP.NET MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在铁轨我可以为了封装给定的名称里面的观点(或URL preFIX)创建命名空间

In rails I could create a namespace in order to encapsulate views inside a given name ( or URL prefix)

我想要做的就是创建一个命名空间(或地区相信?)那须封装一个给定的名字里面的所有管理员的控制器。

What I want to do is create a namespace (or Area I believe? ) that shall encapsulate all the administrator controllers inside a given name.

例如,我想创建一个管理员命名空间,在这里,每当我去www.myapp.com/admin/~~V它会得到我用指数法控制器管理,而每当我去www.myapp。 COM /管理/产品应当与索引方法调用产品控制器等,因为我也想限制这些控制器必须登录为一个人。

For example, I want to create an Admin namespace, where whenever I go to www.myapp.com/admin/ it would get the me the controller admin with the index method, and that whenever I go to www.myapp.com/admin/products it shall call the product controller with the index method and so on because i also want to limit these controllers to a person that must be logged in as in.

URL和路由明智的,我怎么能完成之前提到的?

URL and routing wise, how can I accomplish the mentioned before?

推荐答案

该功能INFACT的名为在asp.net mvc的。

The feature infact is called Areas in asp.net mvc.

您右键单击在Visual Studio中的项目,然后单击添加区。

You right-click your project in Visual Studio and click add Area.

您现在必须与视图,控制器和一个共享文件夹文件夹的子文件夹。还一个路由添加到项目

You'll now have a sub folder with folders for Views, Controllers and a Shared folder. Also a route is added to the project.

障碍:
有一个地方,如果你有这将导致出现问题的情况下的的HomeController 里面的地区之一,因为它会与的HomeController 为网站根路径。史蒂芬·桑德森已定为这在他的书:

Snag: There is a case where it would cause a problem if you have a HomeController inside one of your areas as it will conflict with the HomeController route for the website root. Steven Sanderson has fix for this in his book:

更改默认路由是:

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", // Parameter defaults
id = UrlParameter.Optional },
new [] { "MyAppName.Controllers" } // Prioritized namespace
);

请参阅 MDSN文章的。

视频在Asp.net领域。

由史蒂芬·桑德森好文章:

这篇关于命名空间相当于ASP.NET MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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