每个请求创建一个类似 UrlHelper 的帮助器类实例?
我想创建一个与 UrlHelper 类类似的帮助器类,因为它需要该类的实例化才能对视图可用,但我一生都无法弄清楚在哪里实例化 UrlHelper 才能在视图中可用。查看@Url,因为我想做同样的事情。
有人能启发我吗?
I would like to create a helper class which is similar to the UrlHelper class in that it requires an instantiation of the class to be available to the views, but I cant for the life of me work out where UrlHelper is instantiated to become available in the view as @Url, as I would like to do the same thing.
Can anyone enlighten me at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我以前从未这样做过,但你可以尝试一下。
尝试创建一个扩展 System.Web.Mvc.ViewPage 的新类。我在这台机器上没有 MVC 源代码,所以我不能确切地说框架如何为视图创建此实例,但您也许可以更改 Web.config 中的基本页面类型以使 MVC 创建新类型的实例而不是它自己的类型。
然后,您可以将所需的任何属性添加到该新类型中,并在您的视图中访问它们。
您还必须为该类型创建一个泛型:
Your.New.Page.Type
,并将其声明为您视图中的类型(我不太确定这是怎么回事)在 Razor 中完成):我可能缺少很多东西。
请参阅
pageBaseType
属性:I've never done this before, but it's something you could try.
Try creating a new class that extends System.Web.Mvc.ViewPage. I don't have the MVC source on this machine, so I can't say exactly how the framework creates this instance for the views, but you may be able to change the base page-type in your Web.config to make MVC create instances of your new type rather than its own type.
You could then add whatever properties you want to that new type and have them accessible in your views.
You'd also have to create a generic for that type:
Your.New.Page.Type<T>
, and declare that as the type in your view (I'm not exactly sure how this is done in Razor):There's probably a bunch of stuff I'm missing.
See the
pageBaseType
attribute:Controller的UrlHelper是在MVC框架本身中设置的。向视图中可用的控制器添加另一个对象级属性需要破解框架(如果我错了,请告诉我)。
一般来说,最好创建一个扩展方法来返回视图中所需的对象。例如:
要像这样使用
GetMyObject()
:@Url.GetMyObject()
,您需要在 Web.config 中包含声明扩展方法的命名空间:更新:
为了回应您的评论,请考虑向您的模型添加非视图特定功能:
The Controller's UrlHelper is set in the MVC framework itself. Adding another object-level property to the controller available in views would require hacking the framework (if I'm wrong, let me know).
Generally, it's a better idea to create an extension method which returns the object you want in your view. For example:
To use
GetMyObject()
like this:@Url.GetMyObject()
, you need to include the namespace in which the extension method is declared in your Web.config:Update:
In response to your comment, consider adding non-View specific functionality to your model:
像这样创建你的助手:并且在你的视图中你可以像这样引用它:
如果它们位于不同的命名空间中,你需要将其放在视图的顶部:
编辑:
如果您不想扩展
UrlHelper
类,您可以创建一个单独的类,如下所示:只要您的视图中有此引用,您就可以像这样调用这个方法:
Create your helper like this:And in your view you can reference it like this:
If they are in different namespaces, you need to have this at the top of your view:
EDIT:
If you don't want to extend the
UrlHelper
class, you could make a separate class like this:As long as you have this reference in your View, you can call this method like so:
我唯一能想到的就是在 UrlHelper 上创建一个扩展并在那里实现您的需求。
然而问题仍然是您想用它做什么,因为在某些情况下编写 AjaxExtension 或 HtmlExtension 会更容易。这就是我对你的问题做出最初评论的原因。所有这些都让您能够接触到不同的概念。
The only thing I can think of is creating a extension on the UrlHelper and implement your needs there.
However the question still is what would you like to do with it, because in some cases it would be easier to write an AjaxExtension or an HtmlExtension. That was the reason for my initial comment on your question. All provide you access to different concepts.