每个请求创建一个类似 UrlHelper 的帮助器类实例?

发布于 2025-01-08 01:13:49 字数 124 浏览 0 评论 0原文

我想创建一个与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

表情可笑 2025-01-15 01:13:49

我以前从未这样做过,但你可以尝试一下。

尝试创建一个扩展 System.Web.Mvc.ViewPage 的新类。我在这台机器上没有 MVC 源代码,所以我不能确切地说框架如何为视图创建此实例,但您也许可以更改 Web.config 中的基本页面类型以使 MVC 创建新类型的实例而不是它自己的类型。

然后,您可以将所需的任何属性添加到该新类型中,并在您的视图中访问它们。

您还必须为该类型创建一个泛型:Your.New.Page.Type,并将其声明为您视图中的类型(我不太确定这是怎么回事)在 Razor 中完成):

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="Youre.New.Page.Type" %>

我可能缺少很多东西。

请参阅 pageBaseType 属性:

<configuration>
  <system.web>

    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="Your.New.Page.Type, Your.New"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    </pages>
  </system.web>
</configuration>

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):

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="Youre.New.Page.Type" %>

There's probably a bunch of stuff I'm missing.

See the pageBaseType attribute:

<configuration>
  <system.web>

    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="Your.New.Page.Type, Your.New"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    </pages>
  </system.web>
</configuration>
浮萍、无处依 2025-01-15 01:13:49

Controller的UrlHelper是在MVC框架本身中设置的。向视图中可用的控制器添加另一个对象级属性需要破解框架(如果我错了,请告诉我)。

一般来说,最好创建一个扩展方法来返回视图中所需的对象。例如:

public static class UrlHelperExtensions
{
    public static MyObjectType GetMyObject(this UrlHelper urlHelper)
    {
        return new MyObjectType();
    }
}

要像这样使用 GetMyObject()@Url.GetMyObject(),您需要在 Web.config 中包含声明扩展方法的命名空间:

<namespaces>
    <!-- other namespaces will be here -->
    <add namespace="My.Namespace" />
</namespaces>

更新:
为了回应您的评论,请考虑向您的模型添加非视图特定功能:

@Model.MyObject.GetSomeProperty()

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:

public static class UrlHelperExtensions
{
    public static MyObjectType GetMyObject(this UrlHelper urlHelper)
    {
        return new MyObjectType();
    }
}

To use GetMyObject() like this: @Url.GetMyObject(), you need to include the namespace in which the extension method is declared in your Web.config:

<namespaces>
    <!-- other namespaces will be here -->
    <add namespace="My.Namespace" />
</namespaces>

Update:
In response to your comment, consider adding non-View specific functionality to your model:

@Model.MyObject.GetSomeProperty()
最初的梦 2025-01-15 01:13:49

像这样创建你的助手:

public static class Extensions
{
    public static string SomeUrl(this UrlHelper urlHelper)
    {
        return "someUrl";
    }
}

并且在你的视图中你可以像这样引用它:

@Url.SomeUrl()

如果它们位于不同的命名空间中,你需要将其放在视图的顶部:

@using YourContainingExtNameSpace

编辑

如果您不想扩展UrlHelper类,您可以创建一个单独的类,如下所示:

public class UrlGenerator
{
    public string MakeUrl()
    {
        return "someUrl";
    }
}

只要您的视图中有此引用,您就可以像这样调用这个方法:

@{
    UrlGenerator ug = new UrlGenerator();
    ug.MakeUrl();
}

Create your helper like this:

public static class Extensions
{
    public static string SomeUrl(this UrlHelper urlHelper)
    {
        return "someUrl";
    }
}

And in your view you can reference it like this:

@Url.SomeUrl()

If they are in different namespaces, you need to have this at the top of your view:

@using YourContainingExtNameSpace

EDIT:

If you don't want to extend the UrlHelper class, you could make a separate class like this:

public class UrlGenerator
{
    public string MakeUrl()
    {
        return "someUrl";
    }
}

As long as you have this reference in your View, you can call this method like so:

@{
    UrlGenerator ug = new UrlGenerator();
    ug.MakeUrl();
}
走野 2025-01-15 01:13:49

我唯一能想到的就是在 UrlHelper 上创建一个扩展并在那里实现您的需求。

public static class UrlExtensions
{
    public static string CustomThingy(this UrlHelper urlHelper)
    {
        //Do your custom stuff in here and add extra parameters if you need them
        //For example some Func or Expression or more simpler a string
    }
}

然而问题仍然是您想用它做什么,因为在某些情况下编写 AjaxExtension 或 HtmlExtension 会更容易。这就是我对你的问题做出最初评论的原因。所有这些都让您能够接触到不同的概念。

public static class HtmlExtensions
{
    public static string CustomThingy(this HtmlHelper urlHelper)
    {

    }
}

public static class AjaxExtensions
{
    public static string CustomThingy(this AjaxHelper urlHelper)
    {

    }
}

The only thing I can think of is creating a extension on the UrlHelper and implement your needs there.

public static class UrlExtensions
{
    public static string CustomThingy(this UrlHelper urlHelper)
    {
        //Do your custom stuff in here and add extra parameters if you need them
        //For example some Func or Expression or more simpler a string
    }
}

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.

public static class HtmlExtensions
{
    public static string CustomThingy(this HtmlHelper urlHelper)
    {

    }
}

public static class AjaxExtensions
{
    public static string CustomThingy(this AjaxHelper urlHelper)
    {

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文