简体   繁体   English

如何在MVC中为所有视图提供Html助手?

[英]How is Html helper 'available' to all views in MVC?

Perhaps this is a question for Programmers - I'm sure someone will advise me if so. 也许这对程序员来说是一个问题 - 我相信如果有的话,有人会建议我。

When using MVC, I can also access the Html Helper variable and use whatever extension methods are defined, either the .Net ones or ones I might have specified myself. 当使用MVC时,我也可以访问Html Helper变量并使用任何扩展方法,无论是.Net的还是我自己指定的。

@Html.Raw("<h1>This is my HTML</h1>")

How is this achieved? 这是如何实现的? Say I wanted to add a separate object for another reason and have it available to all views in my MVC app? 假设我想为了另一个原因添加一个单独的对象,并将其提供给我的MVC应用程序中的所有视图?

I know I can't add it to the controller, as one view might be accessed by several controllers. 我知道我无法将其添加到控制器,因为一个视图可能被多个控制器访问。

So my best guess is needing to subclass IView? 所以我最好的猜测是需要继承IView?

Edit: sorry - I didn't explain myself. 编辑:抱歉 - 我没有解释自己。 At least I think... 至少我认为......

I want the object to be available, but also instantiated. 我希望对象可用,但也要实例化。 So for example, say I had an object called glosrob , with a method SayHello() and wanted to be able use it thusly: 例如,假设我有一个名为glosrob的对象,使用方法SayHello()并希望能够如此使用它:

@glosrob.SayHello()

Essentially I want to avoid having to add code to construct the object on each view eg avoid this 基本上我想避免在每个视图上添加代码来构造对象,例如避免这种情况

@{
    var glosrob = new Glosrob();
}

How is Html helper 'available' to all views in MVC? 如何在MVC中为所有视图提供Html助手?

Inside your views folder you will find a separate web.config file. 在views文件夹中,您将找到一个单独的web.config文件。 In the config file will be the class that all views derive from: 在配置文件中将是所有视图派生自的类:

<pages pageBaseType="System.Web.Mvc.WebViewPage">

So all views derive from WebViewPage class which contains a property called Html that exposes the HtmlHelper. 因此,所有视图都派生自WebViewPage类,该类包含一个名为Html的属性,用于公开HtmlHelper。

Say I wanted to add a separate object for another reason and have it available to all views in my MVC app? 假设我想为了另一个原因添加一个单独的对象,并将其提供给我的MVC应用程序中的所有视图?

If you want your own custom property exposed you should read up on Changing Base Type Of A Razor View . 如果您想要暴露自己的自定义属性,您应该阅读更改Razor View的基本类型

First you create your own class with your own property: 首先,您使用自己的属性创建自己的类:

public abstract class CustomWebViewPage : WebViewPage {
  public Glosrob Glosrob { get; set; }

  public override void InitHelpers() {
    base.InitHelpers();
    Glosrob = new Glosrob ();
  }
}

And update your config: 并更新您的配置:

<pages pageBaseType="MyProject.Web.CustomWebViewPage">

Then you can call it in your view: 然后你可以在你的视图中调用它:

 @Glosrob.SomeMethod();

Inside your views folder you will find a separate web.config file. 在views文件夹中,您将找到一个单独的web.config文件。 In this file you can declare namespaces that need to be imported. 在此文件中,您可以声明需要导入的名称空间。 The section you're looking for is this: 您正在寻找的部分是这样的:

<system.web.webPages.razor>
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
        </namespaces>
    </pages>
</system.web.webPages.razor>

If you want to import your own objects, you can add any namespace in this section and it will be available in your Razor views 如果要导入自己的对象,可以在此部分中添加任何命名空间,它将在您的Razor视图中可用

EDIT: In case you need a specific instance for each view, you'll need to provide your own view implementation. 编辑:如果您需要每个视图的特定实例,您需要提供自己的视图实现。 You can do this by inheriting from WebViewPage: 您可以通过继承WebViewPage来完成此操作:

public class MyViewPage : WebViewPage
{
    // implement property here
}

public class MyViewPage<TModel> : WebViewPage<TModel>
{
    // implement property here
}

Note that you need to provide an implementation for the generic AND non-generic version. 请注意,您需要为通用AND非泛型版本提供实现。 After that you need to make sure that the View Engine uses this as the base page. 之后,您需要确保View Engine将其用作基页。 In the section mentioned above you would do the following: 在上面提到的部分中,您将执行以下操作:

<system.web.webPages.razor>
    <pages pageBaseType="<the.name.space>.MyViewPage">
    ...
    </pages>
</system.web.webPages.razor>

In case you can do with a shared object, you could define it in a static class and make that class available using the above method. 如果您可以使用共享对象,则可以在静态类中定义它,并使用上述方法使该类可用。

Well you have a couple of options. 那么你有几个选择。 You could put your Html Helper class in the System.Web.Mvc namespace like 您可以将您的Html Helper类放在System.Web.Mvc命名空间中

    namespace System.Web.Mvc
{
    public static class HtmlExtensions
    {

You could also import the namespace on each page: 您还可以在每个页面上导入命名空间:

<%@ Import Namespace="MvcApplication1.Helpers" %>

Or you could register in the web.config 或者您可以在web.config中注册

<namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
    <add namespace="MyHelper"/>

  </namespaces>

Also check this as a possible duplicate: Import Namespace for Razor View 同时将此选项视为可能的重复: 为Razor View导入命名空间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM