ASP.NET MVC - 设置的ViewData在基本控制器母版 [英] ASP.NET MVC - Set ViewData for masterpage in base controller
问题描述
我用我的ASP.NET MVC项目中的母版。该母版预计一些的ViewData是present,它显示此每一页上。
I'm using a masterpage in my ASP.NET MVC project. This masterpage expects some ViewData to be present, which displays this on every page.
如果我没有设置我的控制器这个ViewData的关键,我得到一个错误,它无法找到它。不过,我不希望设置ViewData的每个控制器(我不想说计算机[富] =的getFoo();
在每个控制器)
If I don't set this ViewData key in my controllers, I get an error that it can't find it. However, I don't want to set the ViewData in every controller (I don't want to say ViewData["foo"] = GetFoo();
in every controller).
所以,我在想一个基本的控制器设置此,并且拥有每个控制器都是从这个基本控制器继承。在基地控制器默认constructur,我设置的ViewData。我发现了一个类似的方法在这里:<一href=\"http://www.asp.net/learn/MVC/tutorial-13-cs.aspx\">http://www.asp.net/learn/MVC/tutorial-13-cs.aspx.到目前为止好,这个工程......但问题是,这个数据来自数据库的某个地方。
So, I was thinking of setting this in a base controller, and have every controller inherit from this base controller. In the base controller default constructur, I set the ViewData. I found a similar approach here: http://www.asp.net/learn/MVC/tutorial-13-cs.aspx. So far so good, this works... but the problem is that this data comes from a database somewhere.
现在,当我想单元测试我的控制器,从基本控制器继承那些调用它的默认构造函数。在默认的构造函数,我初始化我的仓库类从数据库中获取这些数据。结果:我的单元测试失败,因为它不能访问数据(我当然不的希望的他们访问这些数据)
Now when I want to Unit Test my controllers, the ones that inherit from the base controller call its default constructor. In the default constructor, I initialize my repository class to get this data from the database. Result: my unit tests fail, since it can't access the data (and I certainly don't want them to access this data).
我也不想上课传递正确的信息库(或DataContext的,不管你的名字),以每个控制器反过来把它传递给默认的控制器,我可以再与我的单元测试嘲笑。反过来控制器依赖其他存储库类,我最终会传递多个参数构造函数。太多的工作对我的感觉,还是我错了?有另一种解决方案?
I also don't want to pass the correct Repository (or DataContext, whatever you name it) class to every controller which in turn pass it to the default controller, which I could then mock with my unit tests. The controllers in turn rely on other repository classes, and I would end up passing multiple parameters to the constructor. Too much work for my feeling, or am I wrong? Is there another solution?
我使用StructureMap尝试,但最终我没有这样的感觉是要解决我的问题,因为每个控制器仍然必须调用基构造函数将初始化库类,所以我不能嘲笑它。
I've tried using StructureMap but in the end I didn't feel like that is going to fix my problem, since every controller will still have to call the base constructor which will initialize the repository class, so I can't mock it.
<一个href=\"http://stackoverflow.com/questions/700404/passing-viewdata-to-asp-net-mvc-masterpages\">This是一个类似的问题,但我没有找到满意的答案给出。我可以解决这个在一个整洁的方式,也许用StructureMap作为一个解决方案?或者我应该仅仅指刚吮吸它,传递一个信息库,以每个控制器,并再次把它传递给主控制器?再次,这感觉像这么简单的东西这么多的工作。谢谢!
This is a similar question but I find no satisfactory answer was given. Can I solve this in a neat way, maybe using StructureMap as a solution? Or should I jsut suck it and pass a Repository to every controller and pass it again to the base controller? Again, It feels like so much work for something so simple. Thanks!
推荐答案
我看到两个选项:
第一:
设置为ViewData的母版在YourBaseController.OnActionExecuting()或YourBaseController.OnActionExecuted():
Set the ViewData for MasterPage in YourBaseController.OnActionExecuting() or YourBaseController.OnActionExecuted():
public class YourBaseController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
// Optional: Work only for GET request
if (filterContext.RequestContext.HttpContext.Request.RequestType != "GET")
return;
// Optional: Do not work with AjaxRequests
if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
return;
...
filterContext.Controller.ViewData["foo"] = ...
}
}
二:
或创建自定义过滤器:
public class DataForMasterPageAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// Optional: Work only for GET request
if (filterContext.RequestContext.HttpContext.Request.RequestType != "GET")
return;
// Optional: Do not work with AjaxRequests
if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
return;
...
filterContext.Controller.ViewData["foo"] = ...
}
}
然后再应用到你的控制器:
and then apply to your controllers:
[DataForMasterPage]
public class YourController : YourBaseController
{
...
}
我觉得第二个解决方案是完全针对你的情况。
I think the second solution is exactly for your case.
这篇关于ASP.NET MVC - 设置的ViewData在基本控制器母版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!