SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

Matt Ward

ASP.NET MVC Support

One of the new features in SharpDevelop 4.2 is better support for ASP.NET MVC 3 applications. Previous versions contained project templates for ASP.NET MVC but not much more. With SharpDevelop 4.2 the following features have been added:

  • Razor project and file templates for C# and Visual Basic
  • Razor syntax highlighting
  • Add Controller dialog using T4 templates
  • Add View dialog using T4 templates
  • HTML folding

Now let us take at these new features.

Prerequisites

Before using SharpDevelop you should install ASP.NET MVC 3 and IIS, either IIS Express or full IIS. You can get MVC 3 and IIS Express from Microsoft's Web Platform installer or download them directly from the following pages:

After installing IIS Express make sure that you run IISExpress.exe once so it can generate its configuration files for your user profile. If you do not then SharpDevelop will fail to configure IIS Express.

Creating an ASP.NET MVC Application

Now we shall create a new C# ASP.NET MVC application that uses the Razor view engine. First run SharpDevelop and then from the File menu select New Solution to open the New Project dialog.

New ASP.NET MVC project dialog

On the left in Categories select ASP.NET MVC under C#. On the right you will see three project templates.

  • Empty MVC Application - creates an MVC application with a minimal set of files.
  • MVC Application - creates an MVC application that uses the Web Forms view engine (.aspx).
  • Razor MVC Application - creates an MVC application that uses the Razor view engine.

Select the Razor MVC Application project template. Enter a name for the project and select a location where the project will be created. Click Create to create the MVC project.

ASP.NET MVC project in Projects window

If you open the Projects window by selecting Projects from the View menu you will see that the project template created a Home Controller, two Razor view pages - Index and Contact, as well as installing the jQuery and Modernizer NuGet packages.

Syntax Highlighting and Folding

If you open the a Razor file (.cshtml) you will see that there is html folding and Razor syntax highlighting.

Razor syntax highlighting

The Razor syntax highlighting is still under development and does not work in all cases since it is not using Microsoft's Razor parser and uses a basic syntax definition file. In the screenshot above, for example, the Razor @ symbol is not correctly highlighted in the script and link tags.

Running an ASP.NET MVC Application

In order to be able to run your application first you will need to configure the project to use IIS Express or IIS. From the Project menu select Project Options. Select the Debug tab. At the bottom of this tab you will see options for IIS Express and IIS.

IIS project options

For this walkthrough we will use IIS Express. Select IIS Express and enter a port number. Then click the Create Applicaton/Virtual Directory button. A message will then be displayed telling you that the application/virtual directory was created successfully. Now we can run the application.

To run the application select Run from the Debug menu. IIS Express will then be launched and your application will be loaded into your default web browser.

ASP.NET MVC application running in browser

You can set breakpoints in your application and debug your code in the standard way. For example, set a breakpoint in the HomeController's Contact method and then click Contacts in your browser and you will see SharpDevelop stop at the breakpoint.

Breakpoint hit in HomeController Contact method

To stop debugging select Stop process from the Debug menu. You will then see a prompt asking you to detach or terminate the process being debugged, in this case IIS Express. Detaching will leave IIS Express running.

File Templates

SharpDevelop includes a set of file templates for ASP.NET MVC applications. You can create a new file using these templates by selecting the project or a folder in the Projects window, right clicking and select Add New Item.

ASP.NET MVC file templates in New File Dialog

Select the appropriate template, give the file a name and click Create to add a new file to your project.

Add Controller Dialog

You can add a new controller to your project using a file template as described in the previous section or you can use the Add Controller dialog.

Select the Controllers folder in the Projects window, right click and select Add Controller to show the Add Controller dialog.

Add controller dialog

In the dialog you specify the controller's name and select one of the templates that will be used to generate the controller class. These templates are defined by a single T4 template that you can edit if you want to customise it. The T4 template exists in the following subfolder where SharpDevelop was installed:

AddIns\BackendBindings\AspNet.Mvc\ItemTemplates

Click the Add button to create a new controller from the T4 template.

Add View Dialog

A new view can be created by using a file template or by using the Add View dialog. Select one of the Views folders, right click and select Add New View to show the Add View dialog.

Add view dialog

In the dialog you can specify the following items:

  • View Name - specifies the name of the view
  • View Engine - selects either web forms (ASPX) or Razor view engine. The options displayed in the dialog will change based on this selection.
  • Strongly Typed - allows you to generate a view based on a type in your project.
  • Model - a list of types in your project. You may have to compile your project for a class to be displayed in this drop down.
  • Template - specifies which of the T4 templates will be used when generating the view. Templates available are - Empty, Create, Delete, Details, Edit, List.
  • Use Layout (Razor) - specifies whether the view will use a layout.
  • Layout (Razor) - specifies the path to the layout page. The browse button allows you to select a layout page from those in your project.
  • User Master Page (ASPX) - specifies whether the view will use a master page.
  • Master Page (ASPX) - specifies the path to the master page. The browse button allows you to select a master page from those in your project.
  • Main Content ID (ASPX) - specifies the main content ID.
  • Partial View - used to generate a partial view.

Views generated from this dialog use T4 templates which exist in the following folder if you need to customise them:

AddIns\BackendBindings\AspNet.Mvc\ItemTemplates

That concludes the walkthrough of the new ASP.NET MVC 3 support in SharpDevelop. Further information on ASP.NET MVC 3 can be found at the Getting Started with ASP.NET MVC page.

Published Jan 29 2012, 08:56 PM by MattWard
Filed under: ,

Comments

 

Eduard said:

By any chance, do you know if someone is looking at this possible issue: community.sharpdevelop.net/.../13643.aspx ?

January 30, 2012 9:13 AM
 

MattWard said:

Yes the issue with having both IIS and IIS Express installed and being able to run the project with either was fixed just before the blog post was published. Give it a try and if there are any issues let me know.

January 30, 2012 10:01 AM
 

ThaiGIS said:

Good stuff.

Looking forward to JavaScript highlight and folding.

Thanks,

ThaiGIS

February 4, 2012 4:45 AM
 

MattWard said:

JavaScript highlighting has been available for a while. Folding has been now been added - community.sharpdevelop.net/.../JavaScriptCodeFolding.aspx

JavaScript code completion is on the todo list.

February 5, 2012 5:02 PM
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.