将常规 C# 代码添加到 SharePoint 2010 网站页面

发布于 2024-11-30 10:50:05 字数 480 浏览 0 评论 0原文

我想将 C# 类添加到现有的 SharePoint 2010 网站页面。通过 SharePoint UI 或在 SharePoint Designer 中创建的网站页面上有一个我想要访问和编辑其属性的 Web 部件。我可以在 Visual Studio 中制作一个不可见的 Web 部件,将其部署在页面上,然后运行我想要以这种方式运行的代码,但我觉得这可能是不好的做法(或者确实如此)?

我谈论的是一般的非 SharePoint .aspx 页面如何引用 C# 文件,如下所示:

<%@ Page language="C#" Inherits="System.Web.UI.Page" CodeFile="Example.cs" %>

是否会将上面这样的行添加到 .aspx 文件中? .cs 文件实际上是如何部署在 SharePoint 网站上的?最后,这可能吗?

我是 SharePoint 和此网站的新手,因此非常感谢所有帮助!

I want to add a C# class to an existing SharePoint 2010 site page. The site page, being created either through the SharePoint UI or in SharePoint Designer, has a web part on it that I want to access and edit its properties. I could just make an invisible web part in Visual Studio, deploy it on the page, and run the code I want to run that way, but I feel like that may be bad practice (or is it)?

I'm kind of talking about how a general non-SharePoint .aspx page can reference a C# file, kind of like below:

<%@ Page language="C#" Inherits="System.Web.UI.Page" CodeFile="Example.cs" %>

Would a line like above be added to the .aspx file? How is the .cs file actually deployed on the SharePoint site? Finally, is this even possible?

I'm new to SharePoint and to this site, so I appreciate all help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

寒江雪… 2024-12-07 10:50:05

默认情况下,出于安全原因,SharePoint 不支持常规页面中的代码隐藏文件(尽管,正如 alfonso 提到的,应用程序页面支持内联代码和文件隐藏代码)。您可以在 web.config 中覆盖此设置,但这并不被认为是一个好的做法。

您当然可以创建另一个 Web 部件来执行您想要的操作。我以前做过类似的事情。但通常,当我想要更改自定义页面的行为时,我会更改 Page 指令的 Inherits 属性:

<%@ Page 
    language="C#" 
    DynamicMasterPageFile="~masterurl/default.master"
    Inherits="MyNamespace.MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" 
    %>

创建您的类,让它继承自 System.Web.UI.Page,然后部署将其提交给 GAC。这不仅受支持,而且大多数现成的 SharePoint 页面都是这样做的。

By default, SharePoint does not support code behind files in regular pages for security reasons (although, as alfonso mentioned, inline code and code behind files are supported by application pages). You can override this setting in the web.config, but that is not considered to be a good practice.

You certainly could create another web part to do what you want. I have done something similar to that before. But typically, when I want to change the behavior of a custom page, I change the Inherits attribute of the Page directive:

<%@ Page 
    language="C#" 
    DynamicMasterPageFile="~masterurl/default.master"
    Inherits="MyNamespace.MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" 
    %>

Create your class, have it inherit from System.Web.UI.Page, then deploy it to the GAC. Not only is this supported, but this is how most of the out of the box SharePoint pages do it.

尤怨 2024-12-07 10:50:05

我认为没有“正确”的方法可以通过网站页面来执行此操作。唯一的方法是使用您所说的 Web 部件或用户控件。但是,您可以非常轻松地将代码添加到应用程序页面这个人解释得很好

I think there's no "correct" way to do this with a site page. The only way would be using webparts as you said, or user controls. However, you can add code very easily to an application page. This guy explains this very well.

永不分离 2024-12-07 10:50:05

您应该查看 MSDN 上的 SharePoint 开发人员模块

http://www.mssharepointdeveloper.com

有各种如何自定义 SharePoint 2010 的方法如前面已经提到的,您可以使用

  • WebParts
  • ApplicationPages 或 LayoutsPages
  • 自定义 MasterPages
  • DelegateControls

有更多方法可以扩展 SharePoint,因此这实际上取决于您的情况或您的要求来实现。

You should have a look at the developer modules for SharePoint on MSDN

http://www.mssharepointdeveloper.com

There are various ways how to customize SharePoint 2010 as already mentioned earlier here you could use

  • WebParts
  • ApplicationPages or LayoutsPages
  • Custom MasterPages
  • DelegateControls

There are more ways how to extend SharePoint, so it really depends on your situation or on the requirement you've to achieve.

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