Here are a couple of simple methods you can use to encode/decode.
The encoded value is not secure, and as you can see, decoding it is trivial. If your goal is to obfuscate the id, this will work. If you need to secure it, you should take a different approach.
public string Encode( string encodeMe )
{
byte[] encoded = System.Text.Encoding.UTF8.GetBytes( encodeMe );
return Convert.ToBase64String( encoded );
}
public static string Decode( string decodeMe )
{
byte[] encoded = Convert.FromBase64String( decodeMe );
return System.Text.Encoding.UTF8.GetString( encoded );
}
So you could place these methods in your controller, and pass the encoded TechId to the view with viewBag
int techId = 1;
var encoded = Encode(id.ToString());
ViewBag.Encoded = encoded;
And then to use it in your link
@Html.ActionLink(techName, "Details","Home", new { TopicID = ViewBag.Encoded },null)
(Though, you should really consider using a view model. ViewBag, while a convienent and easy way to pass data to the view, is not considered to be best practice. Becoming comfortable with view models and strongly typed views will make your mvc life much easier in the future. Not to mention, produce cleaner and more maintainable code for those that follow you.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…