Zhixian's Tech Blog

2015-01-10

Can not load type MembershipPasswordAttribute using ASP.NET (Mono)

Filed under: web application development — Tags: , , , — Zhixian @ 08:33:01 am

Note: This blog post does not provide a solution.

Background

Recently, I was trying to build a ASP.NET MVC project using Mono on my Ubuntu machine.
It did not end well.

While building the login page, I got this error message.

Could not load type ‘System.Web.Security.MembershipPasswordAttribute’ from assembly ‘System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’.

Error 500 - Google Chrome_201

The offending code is:

test.mvc.web - Views-Home-Index.cshtml - MonoDevelop_202

As it turns PasswordFor is not the only helper extension that will trigger this error.
TextBoxFor have the same issue as well.

Why this happens

As it turns out, the Mono 4.5 that I am using at this time of writing does not have this implemented yet.

You can view here (http://go-mono.com/status/status.aspx?reference=4.5&profile=4.5&assembly=System.Web).

Mono System.Web in 4.5 vs MS.NET 4.5 - Google Chrome_203

Solution

No good solution to this issue currently.

One possible workaround is not to use the HTML Helper extensions.

Advertisements

2014-12-30

How to install Mono on Ubuntu (Part 2 of 2)

Filed under: development — Tags: , , , , , — Zhixian @ 00:06:12 am

This is continuation of my previous blog post How to install Mono on Ubuntu.
This part focus on testing the installation to make sure that you have the minimal to compile C# source code and to run ASP.NET web applications.

Assumptions

  1. You have installed Apache HTTP Server
  2. You have followed the installation instructions in the previous blog post.
  3. You know how to create folder and files in Ubuntu.
  4. You know how to start a terminal session and run commands in it.
  5. You know how to navigate to other directories using terminal session.

Summary Steps

  1. Compiling and Running helloworld
  2. Create a Apache virtual host

Compiling and Running helloworld

Create a folder to hold your source code. For example, I use “dotnet”.

Projects_134

Inside this folder, create a file call helloworld.cs.

dotnet_135

Edit the contents of this file and replace it with the following:

// Hello1.cs
public class Hello1
{
   public static void Main()
   {
      System.Console.WriteLine("Hello, World!");
   }
}

After saving the file, start a new terminal session and navigate to the directory where you saved the file.

zhixian@SARA: ~-Projects-dotnet_136

The command to run the compiler for C# in Mono is mcs. Compile helloworld.cs by entering the following at the command-line:

mcs helloworld.cs

After running this command, you should see that you have a file call helloworld.exe in your project directory.

zhixian@SARA: ~-Projects-dotnet_137

To test run your application, enter the following command at the command-line.

./helloworld.exe

zhixian@SARA: ~-Projects-dotnet_138

You should see the words “Hello, World!” after you run the command.

Create a Apache virtual host

Now that we are sure the compiler for C# works, we will test if mod_mono can run ASP.NET applications.
What we are going to do at this section is to create a virtual host on Apache to host our ASP.NET application.

Steps

  1. Define ASP.NET application
  2. Configure Apache virtual host

Define ASP.NET application

We are going to create our test application in this section.

Create a folder to hold the contents of your ASP.NET web application.
In my case, I choose to put my file in a directory call zxtech.

Websites_139

Inside this folder, create a file call test.aspx.

zxtech_141

Edit test.aspx and replace the contents with the following:

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>ASP Test Page</title>
  </head>
  <body>
        <form id="form1" runat="server">
          <asp:label id="lbl1" runat="server">ASP Test Page</asp:label>
        </form>
  </body>
</html>

test.aspx (~-Yandex.Disk-Websites-zxtech) - gedit_142

Save the file.
After saving the file, note the full path to the folder (press <CTRL>-L or press the button next to the Location buttons.)

You will need this information when configuring Apache virtual host.

zxtech_140

Configure Apache virtual host

Ok. Here’s where things starts to get a little sticky.
At your terminal session, navigate to Apache HTTP server’s sites directory (its /etc/apache2/sites-available/ by default) by entering the following command at the command-line:

cd /etc/apache2/sites-available/

zhixian@SARA: -etc-apache2-sites-available_143

By default, there should be 2 files in the folder, 000-default.conf and default-ssl.conf.
Make a copy of 000-default.conf for your virtual host (I am picking dev.zxtech.web as my virtual host name) by running the following command:

sudo cp ./000-default.conf ./dev.zxtech.web.conf

zhixian@SARA: -etc-apache2-sites-available_144

After the file is copied, edit the file.
Because the file is in folder owned by the superuser, you need to run the following command to edit the file:

gksudo gedit ./dev.zxtech.web.conf &

zhixian@SARA: -etc-apache2-sites-available_145

After you opened the file, you should see the following:

dev.zxtech.web.conf (-etc-apache2-sites-available) - gedit (as superuser)_146

Things to edit on this file:

  1. ServerName
  2. ServerAdmin (optional)
  3. DocumentRoot (optional)

ServerName refers to the your virtual host name that you will be using. In the below screen shot, this will be dev.zxtech.web.

ServerAdmin refers to an e-mail address that will be used when people needs to contact the server administator.

DocumentRoot refers to the folder where you store your ASP.NET application files. This will be the location of the folder that that you have noted in the previous section. In the below screen shot, this will be /home/zhixian/Yandex.Disk/Websites/zxtech.

After update, the file should look something like the below.

-dev.zxtech.web.conf (-etc-apache2-sites-available) - gedit (as superuser)_147

Things to add on this file:

  1. DirectoryIndex
  2. MonoAutoApplication
  3. AddHandler
  4. MonoServerPath
  5. MonoDebug
  6. MonoSetEnv
  7. MonoApplications
  8. Apache directory configuration
  9. Apache location configuration

DirectoryIndex refers to the default documents that will be use if user navigate to the folder without specifying a specific filename.

MonoAutoApplication refers to using a scheme in mod_mono that allows accounts on a machine to deploy ASP.NET pages without requiring to do any configuration. I prefer to do manual configuration, and so am disabling it here.

AddHandler designates files with specified file extensions to be process by mod_mono.

MonoServerPath allows you to specify the version of Mono runtime to use.
In the below screen shot, we are using the latest mono-server 4 which is equivalent to using ASP.NET 4.0.

MonoSetEnv is use to set an environment variable for use with the Mono run time used to execute the web application.
In the screen shot below, I defined MONO_IOMAP.
MONO_IOMAP is defined to take care of case-sensitivity issues. You can read more about it here.

MonoApplications is use to tell mod_mono that the ASP.NET application exists at the root directory of the web site (/) and this root directory maps (:) to the folder /home/zhixian/Yandex.Disk/Websites/zxtech (hence “/:/home/zhixian/Yandex.Disk/Websites/zxtech”)

Aside from the above Mono configuration, you may need to add an Apache directory configuration section especially if you are are hosting the web application files outside of Apache’s default DocumentRoot location which is /var/www/

 <Directory /home/zhixian/Yandex.Disk/Websites/zxtech>
 Options Indexes FollowSymLinks
 AllowOverride None
 Require all granted
 </Directory>

You will also need to add an Apache location configuration section.

 <Location "/">
 Order allow,deny
 Allow from all
 MonoSetServerAlias dev.zxtech.web
 SetHandler mono
 SetOutputFilter DEFLATE
 SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip dont-vary
 </Location>
 <IfModule mod_deflate.c>
 AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript
 </IfModule>

After you added the sections, your configuration file should look like the below.

dev.zxtech.web.conf (-etc-apache2-sites-available) - gedit (as superuser)_153

After defining the configuration file, you can enable the website.

sudo a2ensite dev.zxtech.web.conf

zhixian@SARA: -etc-apache2-sites-available_151

The last step to get our virtual name hosting working is to edit our hosts located at /etc/hosts

zhixian@SARA: ~_150

In your text editor, add a line

127.0.1.1 dev.zxtech.web

hosts (-etc) - gedit (as superuser)_154

After this is done, you can navigate to http://dev.zxtech.web/test.aspx to test your sample web application.
You should see:

ASP Test Page - Firefox Developer Edition_155

Reference

A nice tutorial to C# can be found here (http://msdn.microsoft.com/en-us/library/aa288463%28v=vs.71%29.aspx)

2014-12-28

How to install Mono on Ubuntu

Filed under: web application development — Tags: , , , , , — Zhixian @ 01:29:12 am

This blog post describes my installation steps to get Mono running on my Ubuntu.

I will write another blog post on testing this installation.

Assumptions

  1. You have Apache HTTP Server installed
  2. You know how to start a terminal session and run commands there.

Summary

  1. Add Mono Project GPG signing key
  2. Add Mono Package Repository
  3. Add mod_mono Repository
  4. Update package cache
  5. Install Mono
  6. Install mod_mono
  7. Enable mod_mono
  8. Reference

Mono Project GPG signing key

This step adds the GPG signing key to your key-ring.
This signing key is used to make sure the installed files are valid.
To add the key to your key ring, start a terminal session and run the following command:

sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

zhixian@SARA: ~_120

Add Mono Package Repository

This step add the Mono package repository into apt-get list of available package repositories.
Enter the following command at the command-line of the terminal session:

echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list

zhixian@SARA: ~_121

Add mod_mono Repository

This step adds the repository for mod_mono to apt-get list of available package repositories.
mod_mono is use by Apache HTTP server to interpret .NET web pages.
To add the repository, run the following command at the command-line:

echo “deb http://download.mono-project.com/repo/debian wheezy-apache24-compat main” | sudo tee -a /etc/apt/sources.list.d/mono-xamarin.list

zhixian@SARA: ~_124

Update package cache

To update the apt-get package cache to use the repositories that you just added, run the following command at the command-line:

apt-get update

zhixian@SARA: ~_125

Install Mono

To install the complete Mono package enter the following at the command-line:

sudo apt-get install mono-complete

zhixian@SARA: ~_126

After the initial listing of the packages that will be installed, you will be given a prompt asking if you would like to proceed install Mono. Enter “Y” to proceed with the installation process.

zhixian@SARA: ~_127

zhixian@SARA: ~_128

Install mod_mono

Run the following command to add mod_mono:

sudo apt-get install libapache2-mod-mono

zhixian@SARA: ~_129

You might see an error exit status at the end of the installation process.
This is due to some configuration issue in the installation process.
I’m not too sure what exactly is the issue but it seems fine to ignore it.

zhixian@SARA: ~_131

Enable mod_mono

mod_mono is enabled by running the following command:

sudo a2enmod mod_mono

zhixian@SARA: ~_133

Reference

http://www.mono-project.com/docs/getting-started/install/linux/

https://help.ubuntu.com/community/ModMono

Blog at WordPress.com.