Wednesday, October 28, 2009

Storing and Retrieving Cookies

Save Cookie


    private void StoreCookie(string visitorId, string visitorName)


    {


        HttpCookie cookie = new HttpCookie("VisitorInfo");


        cookie["VisitorId"] = visitorId;


        cookie["Name"] = visitorName;


        cookie.Expires = DateTime.Now.AddMinutes(3);


        Response.Cookies.Add(cookie);


    }





Retrieve Cookies


    private void RetrieveCookie()


    {


        var cookie = Request.Cookies.Get("VisitorInfo");


        var visitorId = cookie.Values["VisitorId"];


        var name = cookie.Values["Name"];


    }


Monday, October 26, 2009

Castle Windsor Simple Tutorial

Inversion of Control using Castle Windsor

1. Download Castle Windsor here

2. Add the following Libraries to your Project reference


 Castle.Core.dll


 Castle.DynamicProxy2.dll


 Castle.MicroKernel.dll


 Castle.Windsor.dll





3. Add the following to your Web Config


  <configSections>


    <section  name="castle"


             type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/>


  </configSections>


 


  <castle>


    <components>


      <component id="castleTest"


                 service="MyAssembly.ICastleTest, MyAssembly"


                 type="MyAssembly.CastleTest, MyAssembly"/>


    </components>


  </castle>




4. Code implementation


public class CastleTest : ICastleTest


{


    #region ICastleTest Members


 


    public string HelloWorld(string text)


    {


        return "my test is " + text;


    }


 


    #endregion


}


 


public interface ICastleTest


{


    string HelloWorld(string text);


}




5. In your controller


    public ActionResult About()


    {


        WindsorContainer container = new WindsorContainer(new XmlInterpreter());


        ICastleTest service = container.Resolve<ICastleTest>("castleTest");


 


        ViewData["castleTest"] = service.HelloWorld("hello world");


 


        return View();


    }


Thursday, October 15, 2009

MVC Custom Route Problem on Scripts Path

If you encountered this error on your scripts
"A public action method 'Scripts' could not be found on controller"

It means that there is a problem mapping this files.

Since Custom routes changes the Path/Url, your views needs to be aware of this.

To avoid this problem you need to change the script url to this:
<script src="%3C%=%20Url.Content%28">" type="text/javascript"></script>
<script src="%3C%=%20Url.Content%28">" type="text/javascript"></script>
<script src="%3C%=%20Url.Content%28">" type="text/javascript"></script>

It will generate the correct path of your scrips

Tuesday, October 6, 2009

Creating a Custom Membership Provider

Here are the steps on how you can create your own membership provide in asp.net mvc framework:

1. First you need to create a class that inherits the abstract class MembershipProvider and implements all the abtract members.

public class CustomMembershipProvider : MembershipProvider

2. Override the function ValidateUser. Before you do this, I assume that you already created your database table.

 public override bool ValidateUser(string username, string password)
{
var user = (from u in _db.Users
where u.Username == username
&& u.Password == password
select u).SingleOrDefault();

if (user == null)
return false;
else
{
return true;
}
}


There are lot of procedures to override in the membership provider but I will let you do that with yourself.

3. Now, open your Web.Config and change the membership element to this:

<membership defaultprovider="CustomMembershipProvider">
<providers>
<clear/>
<add name="CustomMembershipProvider"
type="MVCApplication1.CustomMembershipProvider"
connectionStringName="ApplicationServices"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
requiresUniqueEmail="false"
passwordFormat="Encrypted"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
passwordStrengthRegularExpression=""
applicationName="/" />
</providers>
</membership>


This are the settings for your membershipprovider.

THATS IT!
To test this you need to add dummy data to your users table.

Thursday, October 1, 2009

subsonic: How to add new datatype support such as timestamp

subsonic makes the timestamp datatype to a String type instead of Byte[].
Follow this steps to add new datatype support:

1. To add new datatype support for your database tables, modify your SQLServer.ttinclude file
and find this function
string GetSysType(string sqlType)

2. Add this piece of codes at the end of the cases
case "timestamp":
sysType= "byte[]";
break;

3. Run Custom Tool. (Right click on the tt files and click run custom tool)

That's it!

Wednesday, September 23, 2009

MVC Application Not Working on IIS 6

Here are the simple steps on how your mvc application run on IIS 6

1.Open your website properties in IIS
2. Home Directory > Configuration
3. Click Configuration and Insert Wildcard application maps
4: Browse your aspnet_isap.dll >> C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll
5. Uncheck "Verify that file exists"
6. Run your MVC Application Again

Note: Do a research on wildcard mappings.