创建简单类并从 cshtml 文件调用方法 [英] Creating and Simple Class and Calling a Method from a cshtml File

查看:65
本文介绍了创建简单类并从 cshtml 文件调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能举一个简单的例子,用一个简单的方法创建一个简单的类,并从 webmatrix 的 cshtml 文件中调用该方法吗?

Could some one give a simple example of creating a simple class with a simple method and calling the method from cshtml file in webmatrix?

我试图弄清楚 WebMatrix 是否可以以面向对象的方式使用,这将允许我利用 razor 语法作为视图引擎替代 MVC 框架,我可以将其与 webforms 应用程序结合使用.在后端拥有 Webforms 的强大功能和生产力,在面向公众的页面上拥有 razor 真的很棒.

I am trying to figure out if WebMatrix could be used in an object oriented way that would allow me to take advantage of the razor sytax as a view engine alternative to the MVC framework that I could use in combination with webforms applications. Having the power and productivity of webforms on the back-end and razor on the public facing pages would be really awesome.

我对 asp.net 的了解非常有限,但 WebMatrix 引起了我的兴趣,但我认为完整的内联方法不是正确的方法.

I have very limited knowledge of asp.net but WebMatrix intrigues me but I don't think the full inline approach is the right way to go.

谢谢!

推荐答案

只需在 App_Code 中放入一个 .cs 文件,内容如下...

Just put a .cs file in App_Code with something like this...

public class Something {
    public void DoIt() {
    }
}

然后在 .cshtml 文件中调用...

Then in a .cshtml file just call...

var something = new Something();
something.DoIt();

您也可以将其设为静态并直接调用Something.DoIt.

You could also just make it static and call Something.DoIt directly.

希望有所帮助.

这篇关于创建简单类并从 cshtml 文件调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆