简体   繁体   中英

Visual Studio: Share Code between ASP and C# project

I'm developing an application which contains two segments:

  1. a C# form application
  2. a ASP.NET web form application.

During development of these two segments(projects), many functions are same and developing them in two different projects is not reasonable since the development time is wasted this way.

So i want to know are there any method to share code/classes which is used in both projects or not? Something like creating projects in same solution or any other methods which does not make problems during publishing of application on Web or in .exe format.

Solution 1 : if you want to expose your API functionality to the multiple applications You can create WebServices and expose them via wsdl file.so that you can consume those services from any application.

Try This Web Services

Solution 2:

You can create a ClassLibrary Project which generates .dll files which can be used from any other application to access the API functions.

Follow the below steps to create the Library Project which contains common functions (API):

Step1 : Create a New ClassLibrary Project with name MyLibraryProject (you can assign any name) as shown below:

在此处输入图片说明

Step2 : Create the Library functions which are commonly used as below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyLibraryProject
{
    public class MathLibraryClass
    {
        public int num1 { get; set; }
        public int num2 { get; set; }
        public int Sum(int n1,int n2)
        {
            num1 = n1;
            num2 = n2;
            return num1 + num2;
        }
    }
}

Step 3: Now build the project to get the MyLibraryProject.dll file in your project folder.

Step 4: Now goto your project folder which contains dll files. the path is either deubug folder or Release folder based on your Build Output Path .

Note : I assume your Build POutput Path is Release Folder

Step 5: Now Goto Release Folder path :

ProjectFolder->MyLibraryProject->MyLibraryProject->bin->Release

Step 6: Now the folder contains dll file shown as below:

在此处输入图片说明

Step 7: Now dll file is generated successfully so you can add this dll file as reference to any of your project to consume those common api functions.

Step 8 Now create a project (here as an example i'm creating console application) which consumes the common functions generated in dll.

Step 9: Now add your dll file as reference by following steps:

Right Click on your newly created project. Select References Select Add Reference...

Select the Browse option from Reference Manager window as shown below:

在此处输入图片说明 Now Select the required dll as shown below:

在此处输入图片说明

Step 10: Now Add the library project namespace in your project by using statement

using MyLibraryProject;

Step 11: Now Write the following code in your console Application to utilize the library functions

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MyLibraryProject;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MathLibraryClass myclass = new MathLibraryClass();
            int sum = myclass.Sum(12, 4);
            Console.WriteLine("sum is {0}", sum);
        }
    }
}

Step 12: Now you can see the below Output: 在此处输入图片说明

Yes - simply put the common code into a class library (dll). This is exactly what class libraries are for - for sharing code between projects.

Just make sure you target the class library to a .Net framework that is compatible with both the forms project and the ASP.NET one.

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