什么是ASP.NET MVC3中Razor View的基类

时间:2022-07-26 22:28:55

I'm trying to have all my views inherit from a custom class so that I can add certain behaviour and values to all pages, but I'm having some issues. I tried subclassing System.Web.Mvc.WebViewPage but I'm forced to implement an Execute procedure that I don't know what it should do. Also, if I try to access the Context variable, I get a null reference (really weird). This leads me to think that I may have the wrong base class....

我试图让我的所有视图继承自定义类,以便我可以向所有页面添加某些行为和值,但我遇到了一些问题。我尝试了继承System.Web.Mvc.WebViewPage,但是我*实现了一个我不知道应该做什么的执行过程。此外,如果我尝试访问Context变量,我得到一个空引用(真的很奇怪)。这让我觉得我可能有错误的基类......

Any thoughts?

有什么想法吗?

1 个解决方案

#1


30  

Diego, System.Web.Mvc.WebViewPage is the right base type (and you should have another class inheriting from System.Web.Mvc.WebViewPage<TModel> if you want strongly-typed views). You should mark your own class as abstract so that you are not forced to implement the Execute method.

Diego,System.Web.Mvc.WebViewPage是正确的基类型(如果你想要强类型视图,你应该有另一个继承自System.Web.Mvc.WebViewPage 的类)。您应该将自己的类标记为抽象类,以便不必强制实现Execute方法。

Update: To configure all your views to use your custom base class, look into the ~\Views\Web.config file. Inside of it there's a Razor-specific section where you can use the pageBaseType attribute to configure your custom type.

更新:要配置所有视图以使用自定义基类,请查看〜\ Views \ Web.config文件。在其中有一个特定于Razor的部分,您可以使用pageBaseType属性来配置自定义类型。

As far as the Context property is concerned, it should be fully initialized once the view is executing. However, it might not be available if you try to access it too early (for example, from your classes constructor). When are you trying to access it?

就Context属性而言,一旦执行视图就应该完全初始化它。但是,如果您尝试过早访问它(例如,从类构造函数中),它可能不可用。你什么时候试图访问它?


The Execute method is something that is provided by the Razor compiler when your view is compiled. For example, given the following view file

编译视图时,Razor编译器会提供Execute方法。例如,给定以下视图文件

Hello @Name!

The Razor compiler will behind the scenes generate the following class (this is a simplification, so the details might be off, but it should convey the point)

Razor编译器将在幕后生成以下类(这是一个简化,所以细节可能会关闭,但它应该传达点)

public class _Some_Generated_Class_Name_ : System.Web.Mvc.WebViewPage {
  public void Execute() {
    Write("Hello ");
    Write(Name);
    Write("!");
  }
}

Then the framework calls the Execute method on your view class and your view gets executed.

然后框架在您的视图类上调用Execute方法,并执行您的视图。

#1


30  

Diego, System.Web.Mvc.WebViewPage is the right base type (and you should have another class inheriting from System.Web.Mvc.WebViewPage<TModel> if you want strongly-typed views). You should mark your own class as abstract so that you are not forced to implement the Execute method.

Diego,System.Web.Mvc.WebViewPage是正确的基类型(如果你想要强类型视图,你应该有另一个继承自System.Web.Mvc.WebViewPage 的类)。您应该将自己的类标记为抽象类,以便不必强制实现Execute方法。

Update: To configure all your views to use your custom base class, look into the ~\Views\Web.config file. Inside of it there's a Razor-specific section where you can use the pageBaseType attribute to configure your custom type.

更新:要配置所有视图以使用自定义基类,请查看〜\ Views \ Web.config文件。在其中有一个特定于Razor的部分,您可以使用pageBaseType属性来配置自定义类型。

As far as the Context property is concerned, it should be fully initialized once the view is executing. However, it might not be available if you try to access it too early (for example, from your classes constructor). When are you trying to access it?

就Context属性而言,一旦执行视图就应该完全初始化它。但是,如果您尝试过早访问它(例如,从类构造函数中),它可能不可用。你什么时候试图访问它?


The Execute method is something that is provided by the Razor compiler when your view is compiled. For example, given the following view file

编译视图时,Razor编译器会提供Execute方法。例如,给定以下视图文件

Hello @Name!

The Razor compiler will behind the scenes generate the following class (this is a simplification, so the details might be off, but it should convey the point)

Razor编译器将在幕后生成以下类(这是一个简化,所以细节可能会关闭,但它应该传达点)

public class _Some_Generated_Class_Name_ : System.Web.Mvc.WebViewPage {
  public void Execute() {
    Write("Hello ");
    Write(Name);
    Write("!");
  }
}

Then the framework calls the Execute method on your view class and your view gets executed.

然后框架在您的视图类上调用Execute方法,并执行您的视图。