4.08.2011

Using MonoDevelop to Create an ASP.NET Web Service

NOTE: instructions below are for MonoDevelop 2.6 Beta 2 - built on 2011-04-06 03:37:58+0000

Getting Started

Create a new ASP.NET Web Application in MonoDevelop:

  1. From the menu, select: File → New → Solution…
  2. Expand C#.
  3. Select ASP.NET → Web Application.
  4. Enter a name for the ASP.NET project that will be created in the solution in Name:.
  5. Change the root location for the solution in Location:, if desired.
  6. Change the name of the root solution in Solution Name:, if desired.

The Results – I

What you have after executing the new ASP.NET Web Application project wizard is a solution containing one ASP.NET Web Application project. In the default project view in MonoDevelop, you'll find the following items:

  • Default.aspx – This is the default web form rendered and presented in the browser when http://<server>:<port>/ is accessed.
    • Default.aspx.cs – This C# file contains the developer-created common code and event handlers which can be used to affect the processing of the form.
    • Default.aspx.designer.cs – This C# file contains the IDE-generated code for the page.
    Both of the above files contain partial class definitions which are compiled together into a single code-behind class at runtime.
  • Global.asax – This file contains the basic information which is available to the entire application.
    • Global.asax.cs – This C# file contains methods and event handlers for the entire application. These include event handlers for: application initialization, application termination, session handling, error handling, and request handling.
  • web.config – This file contains the primary configuration for the application when deployed to a server. A secondary set of configuration options can be found in the server's Machine.config file.

If you go out to the location where you created the solution/project, you will also find a few other files:

  • /<solution name>/<solution name>.sln – This file contains configuration information for the entire "workspace" of projects.
  • /<solution name>/<solution name>.userprefs – This file is generated by MonoDevelop and contains information specific to the user of the MonoDevelop application. The consensus is that this file should not be included in source-control.
  • /<solution name>/<project name>/<project name>.csproj – This file contains configuration information for the particular project.
  • /<solution name>/<project name>/<project name>.pidb – This binary file contains meta-data generated by MonoDevelop. The consensus is also that this file should not be included in source-control.

Setting Up the Web Service

Since I'm only really interested in exposing a web service using this project, I felt that I really don't need the Default.aspx.* or Global.asax.* files as they are currently not doing anything for the project. I deleted all five of the files.

Create a new ASP.NET Web Service file in MonoDevelop:

  1. In the Solution pad, right-click your project and select: Add → New File…
  2. Select ASP.NET → Web Service.
  3. Enter a name for the file that will be created in the project in Name:.

The Results – II

After the ASP.NET web service wizard has completed, you'll find a new asmx file in your project. This file will contain the basic WebService directive, some using statements (System and System.Web.Services), a namespace, and a class definition.

Exposing a Method in the Web Service

Next, you'll want to create a method that you want to expose as a method on your web service. Add the WebMethod attribute to it. Your asmx file will now look something like this:

When you run this project, you can view a simple front-end for the service at http://localhost:8080/<web service file name>.asmx. This web form is created at runtime by the ASP.NET container.

What? No code-behind?

While it is normally a good practice to separate a view from the code which contains its logic, as in an aspx file, there is really no reason to do so in an asmx file as there is no visual component which must be separated from the code.

5 comments:

سنوسي حواري said...

Really thanks, you're genius !!

I kept looking for a full day for such a simple solution, I've created my original project in JavaScript full web application with Ajax and php services in the back regardless of MVC or CVM :p, but for some cutomer requirements, I had to rewrite it in ASP.net ( Which I dis-respect), so I had to replace the php?query= ?? by aspx ones.

Thank a lot pal.

سنوسي حواري said...

Thanks pal,

I've got my eyes pop because of looking for a simple solution to replace the PHP back end for my Javascript/Ajax/PHP application, without replacing my full smart/interactive javascript huge foundation for sake of ASP.net silly bandwidth eater goat way of building static interface and huge blocks of :P

Great work really, thanks.

Sanousy.

Cenk KIZILDAĞ said...

Hi,
I tried as you told but I am getting Server Error in '/' Application and browser address is http://127.0.0.1:8080/. How can I fix this?

Best Regards.

Ryan David Ransford said...

@Cenk: It has been some time since I have taken a look at this stub of a project. I really do not have much experience with debugging problems in Mono/ASP.NET yet.

If you followed the directions as given, you would not receive a response at http://:/, as the Default.aspx(.cs) files have been removed.

Ryan David Ransford said...

@Cenk: The string "http://:/" should be replaced with "http://<server>:<port>/