采用了全asp.net MVC 4项目基本控制器 [英] using a base controller for entire asp.net MVC 4 project

查看:208
本文介绍了采用了全asp.net MVC 4项目基本控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用整个MVC 4项目,我工作的基本控制器。我已经在网上找到关于它并没有真正知道它是对最佳做法或相互矛盾的观点,如果它是个人的preference只是一个问题。

I am thinking of using a base controller for the entire MVC 4 project that I am working on. I have found conflicting views online about it and not really sure if it is against best practices or if it is just a matter of personal preference.

下面是一个计算器后,指出<一href="http://stackoverflow.com/questions/6119206/what-are-good-candidates-for-base-controller-class-in-asp-net-mvc/6119341#6119341">dont做到这一点

Here is a stackoverflow post that says dont do it

<一个href="http://odeto$c$c.com/Blogs/scott/archive/2010/06/28/action-filter-versus-controller-base-class.aspx">Here是,已经展示了如何做到像有它的无有害影响的职位。 这里和<一href="http://stackoverflow.com/questions/11892294/inheriting-a-base-controller-with-constructor">here以及他们解释它的用法,没有人真正被指出这是不好的做法,或可能导致前进的任何问题。

Here is a post that has shown how to do it like there are no harmful effects of it. Here and here as well they explain its usage where no one really is pointing out that it is bad practice or could lead to any issues going forward.

那么,什么是真正使用在MVC 4项目的几个基地控制器的看法?好?坏?

So what really is the view on using a couple of base controllers in an MVC 4 project? Good? Bad?

修改

我也想指出的是,我对使用一个主控制器直接目的是为了让我能有授权做过一个控制器,并让所有的控制器不需要有授权的属性。我会创建单独的基本控制器为每个角色。由于角色永远不会改变,我将永远需要创建另一个角色另一个基本控制器。你觉得这种方式将有关设计的控制器?

I'd also like to point out that my immediate goal for using a base controller is so that I can have the Authorization done in one controller and so that all the controllers dont need to have the Authorize attribute. I will create separate base controllers for each role. Since the roles are never going to change I will never need to create another base controller for another role. What do you think of this way of going about designing the controllers?

感谢您的时间。

推荐答案

我用的时候用的东西打交道就像覆盖用户主体(在这里看到前一个基本控制器我的一个老问题,描述的理念:<一href="http://stackoverflow.com/questions/8263845/is-this-custom-principal-in-base-controller-asp-net-mvc-3-terribly-inefficient">Is在基本控制器ASP.NET MVC这个自定义用户3非常低效?)。

I have used a base controller before when dealing with things like overriding the User principal (see here for an old question of mine describing the idea: Is this Custom Principal in Base Controller ASP.NET MVC 3 terribly inefficient?).

老实说,我不认为这样做的更好的办法,所以我觉得在这种情况下的使用基本控制器可以是一个很好的事情。

I honestly couldn't think of a better way of doing this, so I feel that in this kind of scenario using a base controller can be a good thing.

我可能不会有不同的基地控制器不同的授权角色,但因为它是相当简单的(少code)只是装饰控制器 [授权(角色=无所谓 )] 键,它会更容易,看看到底发生了什么。

I probably wouldn't have different base controllers for different authorization roles though as it is fairly simple (and less code) just to decorate the controller with [Authorize(Roles="whatever")] and it will be easier to see exactly what is happening.

这可能是值得考虑的一个自定义的 AuthorizeAttribute

It may be worth considering a custom AuthorizeAttribute.

这篇关于采用了全asp.net MVC 4项目基本控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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