简体   繁体   中英

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. 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.

I am fairly new using asp.net, so a simple example would be nice. 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. After that you should be able to access that class throughout the project

Separate your business logic into at least the App_Code folder. 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. 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. 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.

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. Code behind files (.aspx.cs and .aspx.vb) are the exception. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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