Home ยป Technology | Useful tips

Integrate Yet Another Forum - YAF With Existing Website Running On asp.net 4.0

  2. Sep 2011 by Soan

Integrate Yet Another Forum - YAF With Existing Website Running On asp.net 4.0 If you are reading this article, then you probably know about YAF (Yet Another Forum) and looking to use it.

For those who do not know, it is an open source forum (discussions) application written in asp.net and C#. It can be easily integrated with your current website to provide the Forum (Asking questions, getting answers) facility to your website users.

We also use the same forum application to power our community on this site here.

YAF can be used independently or can be integrated with your exisitng website. We are going to talk about the integration with existing website.

Download YAF

The current version of YAF can be downloaded from here. I am using the v1.9.5.5 RTW BIN Zipped YAF. If you want to play with the YAF code or want to customize it, you should download the SRC code zip.

Convert YAF to work on asp.net 4.0 and IIS 7.0

The current version of YAF i.e. 1.9.5 is built on asp.net 3.5. But if your existing website is running on asp.net 4.0, you can easily port YAF to work on asp.net 4.0 too.
No need to make any special adjustments, just follow the steps and it should be good to work on asp.net 4.0. No need to mention that your existing website should already be running on asp.net 4.0.

If you use Godaddy.com to host your website, you may like to follow the instructions here to set up asp.net 4.0 in Godaddy for your exsiting website..

Integration with existing asp.net 4.0 website

  1. Set up Forum in a subfolder:
    The best and clean approach of using Forum is to keep all the code related to Forum in a seaparate subfolder under your root website. For example, if your website code is placed at YOURWEBSITE.com (called root), then we should set up forum at YOURWEBSITE.com/Forum. The word Forum can be anything of your choice.

    So, create a folder Forum in your website's root.
    YOURWEBSITE.com or ~/ --> Stores all your website code. May contain some other folders too.
    YOURWEBSITE.com/Forum/ or ~/Forum/ --> Stores all your YAF Forum code.

  2. Unzip YAF and copy to existing website:
    Unzip the YAF BIN code that you have downloaded. The file structure would look something like this:

    YAF-v1.9.5.5-RTW-BIN\YetAnotherForum.NET\[All Files and folders]

    Copy all files and folders from above path to YOURWEBSITE.com/Forum folder. This brings all YAF code to your existing website and need some folder movements to make it work.

  3. Move [App_*] folders to Root:
    Move (Cut and paste) all [App_*] folders from YOURWEBSITE.com/Forum/ to YOURWEBSITE.com. If you already have these folders in your existing website, you can safely add the YAF contents in same folders.
    YAF would have following folders that need to be moved: App_Browsers, App_Code, App_Data.

  4. Move Bin folder to Root:
    Move (Cut and paste) Bin folder from YOURWEBSITE.com/Forum to YOURWEBSITE.com. If you already have this folder in your existing website, you can safely add the YAF bin's contents in it.

  5. Create Database:
    YAF needs a database to install itself. You can also use a SQL server express 2005 or 2008 to set up YAF. You may already have a database in your existing website. I would advise you to create a new one to keep things separate and clean. Right click 'App_Data' folder, select 'Add New Item'->'SQL Server Database' and to create a new SQL server DB.

  6. Merge web.config files of existing website and YAF:
    This is the most important and time consuming part of integration. YAF has created separate config files for appsettings, mail,db and URL rewriting. If you also have the same set up, then make the following changes in respective files itself.
    Open the existing website's web.config file from ~/ and make the following changes step by step:

    1. Find following tag
      < configuration > < configSections >
      and add the following line (If it contains another tags, let them be there. Don't delete):
      < configuration >
        < configSections >
         <section name="rewriter" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" requirePermission="false"/>
       </ configSections >
      </ configuration >


    2. Find following tag
      < connectionStrings >
      and add the following line (If it contains another tags, let them be there. Don't delete):
      < connectionStrings >
      <add name="yafnet" connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|Database.mdf;" providerName="System.Data.SqlClient"/>
      </ connectionStrings >

      Keep the name of tag as 'yafnet'.
      The connectionString in above tag should point to the database which you created in above steps. Change the name of DB (shown in red) with your DB instance name. I have kept the name as Database.

    3. Find following tag
      < appSettings >
      and add the following line (If it contains another tags, let them be there. Don't delete):
      < appSettings >
      <add key="YAF.ConfigPassword" value="SET A PASSWORD OF YOUR CHOICE"/>
      <add key="YAF.BoardID" value="1"/>
      <add key="YAF.EnableURLRewriting" value="true"/>
      <add key="YAF.UseSMTPSSL" value="false" />
      <add key="YAF.URLRewritingMode" value="Translit" />
      <add key="YAF.DatabaseObjectQualifier" value="yaf_"/>
      <add key="YAF.DatabaseOwner" value="dbo"/>
      <add key="YAF.ConnectionStringName" value="yafnet" />
      <add key="YAF.ProviderExceptionXML" value="ProviderExceptions.xml"/>
      <add key="YAF.AppRoot" value="~/Forum/" />
      <add key="YAF.ProviderKeyType" value="System.Guid"/>
      <add key="YAF.MobileUserAgents" value="iphone,ipad,midp,windows ce,windows phone,android,blackberry,opera mini,mobile,palm,portable,webos,htc,armv,lg/u,elaine,nokia,playstation,symbian,sonyericsson,mmp,hd_mini"/>
      <add key="YAF.RadEditorSkin" value="Black"/>
      <add key="YAF.UseRadEditorToolsFile" value="true"/>
      <add key="YAF.RadEditorToolsFile" value="editors/radEditor/toolsFile.xml" />
      </ appSettings >

      The passowrd that you specify here will be asked by YAF while setting up the databse schema.
      The application root i.e. folder where Forum code resides should be ~/Forum

    4. Find following tag
      < configuration >
      and add the following line (If it contains another tags, let them be there. Don't delete):
      < configuration >
        <rewriter >
         <rewrite url="^~/(.+?)?yaf_login?\.aspx(\?(.+))?$" to="~/$1Default.aspx?g=login&$5" processing="stop" />
         <rewrite url="^~/(.+?)?yaf_info?\.aspx\?i=([0-9]+?)&url\=(.+)$" to="~/$1Default.aspx?g=info&i=$2&url=$3" processing="stop" />
         <rewrite url="^~/(.+)?yaf_topics([0-9]+)p([0-9]+)(_([-a-z0-9]+))?\.aspx(\?(.+))?$" to="~/$1Default.aspx?g=topics&f=$2&p=$3&$7" processing="stop" />
         <rewrite url="^~/(.+)?yaf_topics([0-9]+)(_([-a-z0-9]+))?\.aspx(\?(.+))?$" to="~/$1Default.aspx?g=topics&f=$2&$6" processing="stop" />
         <rewrite url="^~/(.+)?yaf_forum([0-9]+)(_([-a-z0-9]+))?\.aspx(\?(.+))?$" to="~/$1Default.aspx?g=forum&c=$2&$6" processing="stop" />
         <rewrite url="^~/(.+)?yaf_postst([0-9]+)p([0-9]+)(_([-a-z0-9]+))?\.aspx(\?(.+))?$" to="~/$1Default.aspx?g=posts&t=$2&p=$3&$7" processing="stop" />
         <rewrite url="^~/(.+)?yaf_postst([0-9]+)(_([-a-z0-9]+))?\.aspx(\?(.+))?$" to="~/$1Default.aspx?g=posts&t=$2&$6" processing="stop" />
         <rewrite url="^~/(.+)?yaf_postsm([0-9]+)(_([-a-z0-9]+))?\.aspx(\?(.+))?$" to="~/$1Default.aspx?g=posts&m=$2&$6" processing="stop" />
         <rewrite url="^~/(.+)?yaf_profile([0-9]+)(_([-a-z0-9]+))?\.aspx(\?(.+))?$" to="~/$1Default.aspx?g=profile&u=$2&$6" processing="stop" />
         <rewrite url="^~/(.+)?yaf_([-0-9a-z_]+?)\.aspx\?(.+)?$" to="~/$1Default.aspx?g=$2&$3" processing="stop" />
         <rewrite url="^~/(.+)?yaf_([-0-9a-z_]+?)\.aspx$" to="~/$1Default.aspx?g=$2" processing="stop" />
       </rewriter>
      </ configuration >
      NOTE: The above tags have been picked up straight from URLRewriter.config. You can also copy them from there for future releases.

    5. Find following tag
      < httpHandlers>
      and add the following line (If it contains another tags, let them be there. Don't delete):
      < httpHandlers>
      <add verb="GET" path="Resource.ashx" type="YAF.YafResourceHandler, YAF" />
      </ httpHandlers>


    6. Find following tag
      < httpModules >
      and add the following line (If it contains another tags, let them be there. Don't delete):
      < httpModules >
      <add name="YafTaskModule" type="YAF.Core.YafTaskModule, YAF.Core" />
      </ httpModules >


    7. Find following tag
      < pages > < controls > :
      and add the following line (If it contains another tags, let them be there. Don't delete):
      < pages >
        < controls >
         < add tagPrefix="YAF" namespace="YAF.Controls" assembly="YAF.Controls"/>
         <add tagPrefix="YAF" namespace="YAF.Controls.Statistics" assembly="YAF.Controls"/>
         <add tagPrefix="YAF" namespace="YAF.Classes"/>
         <add tagPrefix="YAF" namespace="YAF"/>
       </ controls >
      </ pages >


    8. Find following tag
      < system.webServer > < modules > :
      and add the following line (If it contains another tags, let them be there. Don't delete):
      < system.webServer >
        < modules >
         < remove name="YafTaskModule" / >
         <add name="YafTaskModule" type="YAF.Core.YafTaskModule, YAF.Core" preCondition="managedHandler"/>
       </ modules >
      </ system.webServer >


    9. Find following tag
      < system.webServer > < handlers > :
      and add the following line (If it contains another tags, let them be there. Don't delete):
      < system.webServer >
        < handlers >
         < add name="YafHandler" preCondition="integratedMode" verb="GET" path="Resource.ashx" type="YAF.YafResourceHandler, YAF" />
       </ handlers >
      </ system.webServer >


    10. Find following tag
      < system.web >
      and add the following line (If it contains another tags, let them be there. Don't delete):
      < system.web >
         < globalization culture="en-US" uiCulture="en" requestEncoding="UTF-8" responseEncoding="UTF-8" fileEncoding="UTF-8"/>
      </ system.web >
      NOTE: This tag may already exist in your web.config. If it is already there, don't add another one.

    11. Existing Asp.net membership and YAF:
      If you already have existing membership (user registration and login/log out functionality) enabled in your existing website, then you do not need any other settings now. You are done. But if you don't have any or want to switch to YAF's membership, you should add following tags for it:
      1. Find following tag
        < system.web >
        and add the following line (If it contains another tags, let them be there. Don't delete):
        < system.web >
        < authentication mode="Forms" >
           < forms name=".YAFNET_Authentication" protection="All" timeout="43200" cookieless="UseCookies"/>
        </ authentication >
        </ system.web >
        NOTE: This tag may already exist in your web.config. If it is already there, REPLACE it with the above one.

      2. Find following tag
        < system.web >
        and add the following line (If it contains another tags, let them be there. Don't delete):
        < system.web >
          < roleManager enabled="true" defaultProvider="YafRoleProvider">
            <providers>
              <clear/>
              <add connectionStringName="yafnet" applicationName="YetAnotherForum" name="YafRoleProvider" type="YAF.Providers.Roles.YafRoleProvider"/>
            </providers >
          </roleManager >
          <membership defaultProvider="YafMembershipProvider" hashAlgorithmType="SHA1" >
            <providers >
            <clear/ >
            <add connectionStringName="yafnet" applicationName="YetAnotherForum" name="YafMembershipProvider" requiresUniqueEmail="true" useSalt="true" type="YAF.Providers.Membership.YafMembershipProvider"/ >
            </providers >
            </membership >
            <profile enabled="true" defaultProvider="YafProfileProvider" inherits="YAF.Utils.YafUserProfile" >
            <providers >
            <clear/ >
            <add connectionStringName="yafnet" applicationName="YetAnotherForum" name="YafProfileProvider" type="YAF.Providers.Profile.YafProfileProvider"/ >
            </providers >
            </profile >
        </ system.web >
        NOTE: These tags may already exist in your web.config. If they are already there, REPLACE them with the above ones.

    Wow..we are done with merging the web.config files. The toughest battle has been won.
  7. Disable or Delete config file in Forum:
    Since we have merged the YAF configuration with existing website, we should delete the config files given in YOURWEBSITE.com/Forum. These files shouldbe either excluded from project or deleted:
    app.config, mail.config, db.config, urlrewriter.config and web.config

  8. Set up Forum as a control:
    By default, YAF has its own ~/Forum/default.aspx and runs inside its own master page. If you are okay with this behavior, you can skip this step.
    OR If you want, you can run the whole forum inside your own master page as a control. Open ~/Forum/default.aspx and replace the code with the following code:
    < %@ Page Title="" Language="C#" MasterPageFile="~/MasterForumPage.master" AutoEventWireup="true" %>
    <%@ Register TagPrefix="YAF" Assembly="YAF" Namespace="YAF" %>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <YAF:Forum runat="server" ID="forum" BoardID="1">
    </YAF:Forum>
    </asp:Content>

    The master page should point to your existing website's master page.
    ContentPlaceHolderID should also point to the one you have in your master page file.

  9. Run:
    You should now run the YAF installation by launching the
    ~/Forum OR YOURWEBSITE.com/Forum
    . This will open the ~/Forum/Default.aspx page and automaticaly divert you to ~/Forum/Install/Default.aspx if you are running it for the first time.

    This will open up a wizard which will ask you for the configuration password (remember you entered it in web.config above). Enter the password and follow the wizard. Once the wizard completes, it will set up the database schema for running YAF.

    That's it. Next time you open the ~/Forum OR YOURWEBSITE.com/Forum
    link, you will see the YAF forum running.


Issues and Solutions

The only issue that i faced was that of Profile provider. It looks like YAF does not want you to use your own Profile provider. It allows you to use your own membership and role provider but throws error if you use a profile provider other than YAF.
So, as a fix to this issue, you should use the profile provider tags mentioned above for YAF. Membership and Role provider can be changed to your choice (Your own or YAF).

That is it. Good luck. If you face any issues, ask in comments and i will be glad to help you.

Would you like to share this article?

QR Code for this page Scan this QR code to open this article in any mobile browser or share with friends.


For more helpful articles like this, subscribe to our free newsletter or stay connected on social networks:

SUBSCRIBE
Subscribe to AM22 tech in Reader or by Email
Sign up for our updates in Email (Free):

 

Have questions? Write into comments or ask in forum