简体   繁体   English

在移动版和桌面版ASP.NET C#Web应用程序(窗体)之间共享代码

[英]Sharing Code Between Mobile and desktop version of ASP.NET C# Web Application (Forms)

I have an existing asp.net C# web application and I am starting to develop a mobile version by creating a sub folder "Mobile" and copying over the folders/files I will be offering on the mobile area. 我有一个现有的asp.net C#Web应用程序,并且通过创建子文件夹“ Mobile”并复制将在移动区域提供的文件夹/文件来开始开发移动版本。 I have things working well with the differ style and sitemap for mobile. 我的手机与其他样式和站点地图配合得很好。

As an example, there are many pages that will provide the same business processes, but there may be some slight changes to the presentation layer between the two, above and beyond the styles or master page. 例如,有许多页面将提供相同的业务流程,但是在样式或母版页面之上和之外,两者之间的表示层可能会稍作更改。 I would like to share the code behind logic for the Login.aspx page between the mobile copy and the desktop copy. 我想在移动副本和桌面副本之间共享Login.aspx页面的逻辑背后的代码。

I am fairly new using asp.net, so a simple example would be nice. 我使用asp.net还是一个新手,所以一个简单的例子就不错了。 I've tried to create a shared class in another folder, but have not been able to reference it properly and compile without errors. 我试图在另一个文件夹中创建一个共享类,但是无法正确引用它并进行编译而没有错误。

Ideas? 有想法吗?

Right mouse click on the project, then Add ASP.NET Folder , add "App_Code" folder and place the class there. 右键单击该项目,然后单击“ Add ASP.NET Folder ,添加“ App_Code”文件夹并将类放在此处。 After that you should be able to access that class throughout the project 之后,您应该可以在整个项目中访问该类

Separate your business logic into at least the App_Code folder. 将您的业务逻辑至少分为App_Code文件夹。 But preferably move it into its own class library project. 但最好将其移至其自己的类库项目中。 Then add a reference from your website/web application project to the class library project. 然后,将您的网站/ Web应用程序项目中的引用添加到类库项目中。 Here's how to do that: Right click your solution in the Solution Explorer, go to Add> New Project. 方法如下:在解决方案资源管理器中右键单击您的解决方案,然后转到“添加”>“新建项目”。 Add a class library project to the solution from here. 从此处将类库项目添加到解决方案中。 Then right click your Site project, click Add Reference, make sure you're on the Projects tab, and select the class library you previously created. 然后,右键单击您的Site项目,单击“添加引用”,确保您位于“项目”选项卡上,然后选择先前创建的类库。 Your business logic goes in the class library project, and your site-specific logic goes in the website project. 您的业​​务逻辑在类库项目中,而特定于站点的逻辑在网站项目中。 You need to add Using statements to your classes or use the full namespace syntax to access classes in the class library. 您需要向类中添加Using语句,或使用完整的命名空间语法来访问类库中的类。

In your site-specific logic, you can have individual pages inherit from a common base class if the pages shares the same page specific logic. 在特定于站点的逻辑中,如果页面共享相同的特定于页面的逻辑,则可以使单个页面从通用基类继承。

 public partial class _Default : System.Web.UI.Page
 {
 //This is all the code behind logic for Default.aspx. This code is in Default.aspx.cs. Notice how it is inheriting from System.Web.UI.Page directly.
 }

change to this... 改成这个...

 public class BasePage : System.Web.UI.Page
 {
 //code common to multiple pages goes here. This file (BasePage.cs) goes in your App_Code folder.
 }

and this... 和这个...

 public class _Default : BasePage
 {
 //Now your default page inherits from BasePage class, and therefore it indirectly inherits from System.Web.UI.Page.
 }

It's probably not a good idea to put .cs files into any folder besides App_Code. 将.cs文件放入App_Code之外的任何文件夹中可能不是一个好主意。 Code behind files (.aspx.cs and .aspx.vb) are the exception. 文件后面的代码(.aspx.cs和.aspx.vb)是例外。 See this: http://www.codeproject.com/Articles/31557/A-Beginner-s-Guide-to-ASP-NET-Application-Folders for an explanation of App_Code and other special folders. 有关App_Code和其他特殊文件夹的说明,请参见: http : //www.codeproject.com/Articles/31557/A-Beginner-s-Guide-to-ASP-NET-Application-Folders

暂无
暂无

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

相关问题 在ASP.NET MVC 5,ASP.NET Web Api 2和Mobile(Xamarin)之间共享代码 - Sharing code between ASP.NET MVC 5, ASP.NET Web Api 2 and Mobile (Xamarin) 如何从背后的代码绑定ASP.NET Web Forms C#应用程序中下拉列表中的“ SelectedValue”? - How to bind the 'SelectedValue' in dropdown in ASP.NET Web Forms C# application from code behind? 将ASP.NET Web窗体应用程序转换为桌面应用程序 - Converting ASP.NET Web Forms application into Desktop Application 如何在同一个ASP.NET Web窗体应用程序中使用c#和vb.net代码? - How can I have c# and vb.net code behinds in the same ASP.NET Web Forms application? 在ASP.NET中的2个Web应用程序之间共享会话 - Sharing of Session between 2 web application in asp.net 如何在同一解决方案中为桌面添加C#应用程序和为Web添加ASP.net应用程序 - How to add a C# application for desktop AND a ASP.net application for web in the same solution ASP.NET C#中的Word Counter桌面应用程序 - Word counter desktop application in ASP.NET C# 如何使用会话在页面C#ASP.NET Web窗体之间传递值 - how to use session to passing values between page c# ASP.NET web Forms 为什么在gridview上进行更新时不会在asp.net c#Web窗体应用程序中更新数据库? - Why update on gridview is not updating the database in my asp.net c# web forms application? 如何以所有形式在ASP.NET,C#Web应用程序中链接模块 - How to link a module in asp.net, C# web application in all forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM